Code Schema for Auto-Refresh Pages

I want to communicate with information in real time, and there is a fairly standard functionality that I want to duplicate:

This happens here when you are on the same question, typing your answer, and a warning appears that says: "There are 3 news replies, click to show"

This also happens on Twitter. "There are 5 new tweets in this search: click to update"

I am pretty good at server and client code, and what I'm looking for is the main outline (not even psuedo code, but maybe plain English) about how this happens.

Is there a CRON job on a server running every minute that picks up an AJAX bit with a long poll on the page?

Is the server polling page itself?

Any solutions are welcome. Thanks!

+4
source share
3 answers

You can implement this using an AJAX call that runs on the client side at regular intervals using the Javascript setTimeout . You will have a Javascript function that calls your server-side method, which checks if an update has occurred, displays any update, and then calls setTimeout to call itself.

pseudo code:

 function updateCheck() { //make ajax call //do something if any update has occurred setTimeout("updateCheck()", 10000); //second param is in milliseconds } 
+1
source

From head to toe, I would do it through javascript - set timeouts to question the server. This is only an educated guess.

+1
source

It looks like SO is using periodic updates to make an ajax request to the url, for example:

https://stackoverflow.com/posts/2307584/answer-activity-heartbeat

This returns the result of JSON:

 {"Result":false,"Count":0} 

Here is an example result when a new answer exists:

 {"Result":true,"Count":1} 
0
source

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


All Articles