Is a notification system using setInterval + AJAX bad? [closed]

3

Because they are multiple requests to php + myql, little intervals (milliseconds thing) multiplied by several online users at the same time, is this impractical in the sense of computational resources?

Will these XHR's all give a "bottleneck" to the server, or is it quiet?

If yes, what other alternative do I have? Sockets? (I do not understand this)

    
asked by anonymous 24.03.2017 / 17:18

2 answers

2

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

In addition you will have to have a very good 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 the same WebSockets. You keep a single connection open and only update the client when you receive an event notifying you that a change has occurred.

I do not work with PHP but apparently there are libs that make it easier to adopt WebSockets.

    
24.03.2017 / 18:45
0

It depends a lot on the purpose of your application. If the requests will be made in small intervals (milliseconds), it would be best to work with nodejs, since it maintains a direct connection with the server and allows the changes to be made in real time. Find out about NodeJS and see how to implement it in your project!

    
24.03.2017 / 18:48