A notification system using setInterval + AJAX is bad?

Asked

Viewed 146 times

3

Because there are several requests to php+myql, little interval (millisecond thing), multiplied by several users online at the same time, this is not feasible, in the sense of computational resources?

These XHR’s would all give "neck" on the server, or it’s quiet?

If so, what alternative do I have? Sockets? (I don’t understand that)

  • 3

    It all depends on the number of accesses and how much resource is available. From personal experience, not only does it "choke" as it tends to. Inclusive here I talked about my problem. But each case is a case, the difference is that with SSE (Server-Sent Events) or Websockets you keep only one connection and only "send data" when you actually have something new. If you don’t want to use SSE or Websockets you have Ajax Long Polling, which reduces the number of requests. But, using Ajax Polling is not on.

  • Please, if you can get some content or post tutorial here tbm... I am researching on the subject and having difficulties to understand what to look for or where to look about the related content.

2 answers

2

If the setInterval is too short (milliseconds) you start to run the risk of having inconsistencies. Requests can start arriving in orders other than those that were executed and this can result in an outdated data overwriting an updated one. AJAX Pooling works best for longer ranges.

In addition you will have to have a well-rounded infrastructure to handle the volume of requests. Imagine that each user will make 5 requests per second if the range is 200ms for example.

The ideal would be to work with Websockets anyway. You keep a single connection open and only update the client when you receive an event by notifying that a change has occurred.

I don’t work with PHP but apparently there are libs that facilitate the adoption of Websockets.

  • +1, but only the latest browsers support websockets.

  • @Washingtondacosta actually if you ignore the IE 9 down which in many cases is already quite acceptable you get a pretty cool support. http://caniuse.com/#feat=websockets

  • Basically if you limit by TLS 1.2, which is recommended by PCI Security Standards, automatically everyone accessing the site will be compatible with Websockets, except Opera Mini. I think incompatibility is not so great, it is even more compatible than SSE.

0

It depends very much on the purpose of its application. If the requests will be made in small intervals of time (milliseconds), the most suitable would be to work with nodejs, because this maintains a direct connection to the server and allows the changes to be made in real time. Research about Nodejs and see how to implement in your project!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.