Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX Polling Question - Blocking Or Frequent?

I have a web application that relies on very "live" data - so it needs an update every 1 second if something has changed.

I was wondering what the pros and cons of the following solutions are.

Solution 1 - Poll A Lot

So every 1 second, I send a request to the server and get back some data. Once I have the data, I wait for 1 second before doing it all again. I would detect client-side if the state had changed and take action appropriately.

Solution 2 - Block A Lot

So I start a request to the server that will time-out after 30 seconds. The server keeps an eye on the data on the server by checking it once per second. If the server notices the data has changed it sends the data back to the client, which takes action appropriately.

Scenario

Essentially, the data is reasonably small in size, but changes at random intervals based on live events. The thing is, the web UI will be running something in the region of 2,000 instances, so do I have 2,000 requests per second coming from the UI or do I have 2,000 long-running requests that take up to 30 seconds?

Help and advice would be much appreciated, especially if you have worked with AJAX requests under similar volumes.

like image 576
Fenton Avatar asked May 07 '11 14:05

Fenton


2 Answers

One common solution for such cases is to use static json files. Server-side scripts update them when the data is changed and they are served by fast and light webserver (like nginx). Since files are static and small - webserver will do that right in cache, in very fast manner.

like image 200
zerkms Avatar answered Oct 22 '22 23:10

zerkms


Consider a better architecture. Implementing this kind of messaging system is trivial to do right in something like nodeJS. Message dispatch will be instantaneous, and you won't need to poll for your data on either side.

You don't need to rewrite your whole system: The data producer could simply POST the updates to the nodeJS server instead of writing them to a file, and as a bonus, you don't even need to waste time on disk IO.

If you started without knowing any nodeJS, you could still be done in a couple hours, because you can just hack up the chat example.

like image 30
geocar Avatar answered Oct 23 '22 00:10

geocar