AJAX Poll Question - Blocking or Frequent?

I have a web application that uses very “live” data, so it needs to be updated every 1 second if something has changed.

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

Solution 1 - Poll Lot

So, every 1 second I send a request to the server and return some data. As soon as I have the data, I wait 1 second before doing it again. I would discover the client side if the state changed and took appropriate action.

Solution 2 - Block A Lot

So, I run a request to the server, which will time out after 30 seconds. The server monitors the data on the server, checking it once a second. If the server notices that the data has been changed, it sends the data back to the client, which makes the actions accordingly.

Scenario

Essentially, the data are quite small in size, but vary at random intervals based on live events. Is the fact that the web interface will run something in the 2000 instance area, so do I have 2000 requests per second coming from the user interface or do I have 2000 long requests that take up to 30 seconds?

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

+6
source share
3 answers

One of the common solutions for such cases is to use static json files. Server scripts update them when data changes, and they are served by a fast and easy web server (e.g. nginx). Since the files are static and small, the web server will do this in the cache very quickly.

+2
source

Consider the best architecture. Implementing such a messaging system is trivial to do something like nodeJS . Sending messages will be instant, and you will not need to poll your data from both sides.

You do not need to rewrite the entire system: the data producer could just POST update the nodeJS server instead of writing them to a file, and as a bonus, you don’t even have to spend time on the IO disk.

If you started without knowing the nodeJS host, you can still do it in a couple of hours, because you can just crack the chat example.

+2
source

I can’t comment yet, but I agree with the geocar. Running live or nearly live web services with a simple poll will be a solution stuck between a rock and a hard place.

You can also look into web sockets to enable push, as that sounds better for this than just refreshing every second to 30 seconds.

Good luck

0
source

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


All Articles