Will frequent polling overload the server? If so, what is the best way to implement live updates?

Well, I don’t know which one is the most effective. Can someone help me in finding a better algorithm.

Ok, let's take an example, for example, facebook, when the user sends a message, it will be updated for a friend without refreshing the page, and we know it by ajax request. But how can we know that someone posted a new thing? it may be like setting a timer for every 2 seconds and sending an ajax request for some table and checking that some user has posted something. but is there a way to do this without setting a timer, because executing the operation every 2 seconds can cause a serious problem with the server, I think so? just want to know if there is a better way instead of setting a timer?

Any help is appreciated.

+6
source share
3 answers

Currently, the use of Facebook and Google is a method called a long survey .

This is a simple system whereby a client makes an AJAX request to a server. The server accepts the request and checks if it has the data necessary for the request. If not, the request remains an open but deferred server. The second server has data, the request is processed and returned to the client.

If you open facebook, you will see that requests are sent to Facebook, which take about 55 seconds. The same goes for Gmail and several other web applications that seem to have some kind of push system.

Here is a simple example of how these requests can be handled:

  • Client:

    • AJAX original request that has timestamp 0
  • Server:

    • Compare the request with timestamp 0 by checking the timestamp on the data on the server. Assume that the data on the server has a timestamp 234 .
    • The client fingerprint is different from the current stamp on the server data, so we are returning with the new data.
  • Client:

    • The client receives the data and immediately sends a new AJAX request using timestamp 234 .
    • Then we process the new data and update the web page accordingly.
  • Server:

    • Compares the request with timestamp 234 with the current data label on the server.
    • The stamp values ​​are the same, so we go to sleep.
    • Server data is updated, and the stamp value is now timestamp 235 .
    • Sleeping requests wake up and return with an update.

You can read a more detailed description of more detailed explanations of more modern mechanisms for live updates.

+9
source

Only my two cents, but I had previously solved a similar problem with 2 web services. Basically one with the HaveNewData() line HaveNewData() and the other GetData() (In my case, I had several web services that I wanted to call).

So basically call HaveNewData() on a regular basis (I think that in any case every 2 seconds a bad design and unnecessary) and return a simple 0 or 1 for this method (minimal data). If HaveNewData() returns 1, make your dear webservice call. Alternatively, you can also simply return a value of zero in the main web service when new data is not available, and in my experience this scales at least “reasonably” reasonably.

0
source

A lengthy survey is a great technique, but as with any solution, efficiency depends on many variables, including hardware tuning, and therefore there are no absolute solutions.

Keep in mind that the lengthy survey that you maintain on the connection can cause problems with many clients.

You must consider: -

  • Do I need to be practically in real time (for example, for an exchange trader).
  • How often ajax data / output changes (chat and new comment)
  • How often an event occurs that causes changes in ajax I / O triggers. This will determine how the cache is generated.

You have to be humble when it comes to ajax. The request and response should be based on needs. The success of ajax implementation will not be complete without a well-designed caching solution, which is based on events rather than requests.

The following is a simplified version of one of those methods that we found useful in the project: -

  • Each Ajax polling request made will contain output_hash , which is a digest of data previously returned by the server.
  • The server checks this output_hash for a recent hash of the output that it generated from a data source, preferably cached.
  • If it is different, it will serve the new content along with the new output_hash . Another small answer / Not modified to indicate that there is no new content.

In our solution, we also performed a dynamic calculation of the interval of the next survey. Saving interval dynamics allows the server to control the request. For example, suppose that most comments / responses occur within the first 1 hour, except that there is no time with an interval of 1 second, so the server can increase this to 2.3 or even 5 seconds dynamically with increasing time, but rather tough 2 sec interval encoding Similarly, the interval time can be reduced if a flurry of activity occurs in the old column.

We also checked for non-working customers and more.

0
source

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


All Articles