How to make the server talk to the client

I am new to web development, so excuse my ignorance.

I would like to know if there is a way for the server to send a message to clients. An example of this is the client’s news page, and each time a new story arrives on the server, the server sends this information to the client, and the client updates its news feed. I don’t want the client to constantly poll the server every few seconds, asking, "Hey, is there a new story now? And now, now what?" I want the client to do its job, and then be interrupted by a message from the server.

Is there any way to do this?

+4
source share
2 answers

For newer browsers, you can use web sockets to open a continuous connection to the server, and then the client / server can send messages to each other whenever they want.

For older browsers, as is usually done, the client must “poll” the server to ask the server for some regular schedule if the server has new messages. Typically, the server cannot connect directly to the client due to firewalls, local security settings, an unknown location, etc. Therefore, the client must connect to the server. The survey can be either regular or a survey every 60 seconds with an ajax call to ask if there is something new or there may be a longer poll when the client asks if there is something new and if there is something new, the server returns data immediately. But there is nothing new, the server freezes at the polling request for some period of time, expecting to see if there is something new. In the end, the server will either return that it has nothing, or will return with a new message, if any. When the client receives a response, it starts the sequence of "long polls" again. Comet is an example of a "long survey" in library form, which simplifies its implementation.

+2
source

Pusher - Individual http://pusher.com/

0
source

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


All Articles