AJAX - how to determine the best polling rate?

I am creating new features for a small embedded device with an embedded web server. It has a basic web interface with Javascript AJAX. Today I ran into a problem when I had setInterval calling the AJAX polling function every 500 ms, but in Firebug on the XHR monitor I would see “interrupted” quite a lot. The web interface did not seem to be updated reliably when everything changed on the built-in side. I also noticed in Firebug that loading XHR will take about 500 ms. I changed the AJAX polling rate to 1000 ms and this solved the problem. But if my analysis is correct, the problem may recur if the built-in side starts to load with an accuracy of 1000 ms.

So my question is is there a way to determine the optimal polling rate; where you would like to update the user interface as often as possible, but do not want to overload the server (which is rather slow and limited in my case). And what does “interrupted” on the Firebug XHR network mean?

Thanks Fred

+4
source share
2 answers

Do not use setInterval, initiate another request with setTimeout after receiving the response.

+5
source

500 ms too often. Try 2 seconds and work back as soon as it works. In addition, you may want to do something like starting from 2 seconds, and if it does not work after a certain number of attempts, increase to 5 seconds (or something else). I see it a lot.

Please note that you need to interrogate sucks. If you have a modest user base of 100 concurrent users, and they all use a workflow that requires polling, your application can be flooded with hundreds of requests every second. In other words, the survey does not scale well if you do not have the resources to support the servers horizontally. Depending on your use case, it might be better to just send the initial request to start the long process asynchronously and inform the user about it in 2 minutes.

Let me ask: how long will it take to complete the task for which you are conducting a survey?

+4
source

Source: https://habr.com/ru/post/1334008/


All Articles