Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX - how to determine best polling frequency?

I am building new features for a small embedded device with built in web server. It has a basic web based interface with Javascript AJAX. I came across a problem today where I had a setInterval call an AJAX polling function every 500ms, but in Firebug on the XHR monitoring I'd see "aborted" quite a lot. The web UI did not seem to reliably update when things changed on the embedded side. I noticed also in Firebug that the XHR load would take close to 500 ms. I changed the AJAX polling frequency to 1000ms and that solved the problem. But if my analysis is correct, the problem could reoccur if the embedded side starts taking close to 1000ms to load.

So my question is, is there a way to determine optimal polling frequency; where you'd like to have the UI update as frequently as possible, but don't want to overload the server (which is quite slow and limited in my case). Also what does "aborted" actually mean in the Firebug XHR network view?

Thanks, Fred

like image 859
fred basset Avatar asked Jan 21 '23 19:01

fred basset


2 Answers

Don't use setInterval, initiate another request with setTimeout after you get the response back.

like image 152
epascarello Avatar answered Jan 30 '23 18:01

epascarello


500ms is too often. Try 2 seconds and work backwards once it works. Also, you might want to do something like start with 2 seconds, and if doesn't work after some number of tries, increase to 5 seconds (or whatever). I see that a lot.

Note, having to poll sucks. If you have a modest user base of 100 concurrent users, and they are all using the work flow that requires polling, your application can be flooded with hundreds of requests every second. In other words, polling doesn't scale very well, unless you have the resources to stand up servers horizontally. Depending on your use-case, it might be better to just submit the initial request to start the long running process asynchronously, and tell the user to check back after 2 minutes.

Let me ask this: how long should it take for the task for which you poll to be completed?

like image 29
hvgotcodes Avatar answered Jan 30 '23 19:01

hvgotcodes