It is necessary to display new data recorded in the database without refreshing the page. There are two good examples in my opinion of this implementation:

  • vk (receive new message)
  • gmail (receiving a new letter)

So far I see only such a "dull" way to do it. jquery (ajax, post) updated by setinterval /,

$.post( "/ajaxtest.php", { param1: "param1", param2: 2 }, onAjaxSuccess ); function onAjaxSuccess(data) { // Здесь мы получаем данные, отправленные сервером и выводим их на экран. alert(data); } 

and it's all wrapped in a counter for example every 5 minutes.

Or create a cron on the server with a schedule and every 5 minutes access the script.php

but it seems to me that there are more interesting and correct decisions, maybe someone has come across such a thing, please help, to be honest, I don’t know how else this can be implemented, it’s very interesting. Thanks for attention!

  • it is as if you opened the door to a parallel world, never heard of it, thank you very much, be sure to get acquainted!) Exhaustively! - Teem
  • VK & GMail use long polls (yes, yes, an eternal loop through setTimeout ). And even for new browsers do not throw WebSocket , just checked. - user207618
  • Then I apologize for expressing two good examples in my opinion , I was wrong! vk and settimeout ?! this is a turn))) - Teem
  • @Teem, rendered the answer in response, it can be accepted if it suits you (ticked off under the answer reting). - Duck Learns to Take Cover

1 answer 1

Let's start with the fact that the technologies you need, when the server has to send some data to the browser, are usually combined with the term Comet (although this is not always theoretically true), and if you want to figure it out, you should google it.

There are three main modern ways of implementation:

1. Long polls (long-polling).
Yes, you can simply make an AJAX request by timer (it's called short-polling). Short-polling has problems - not an instant response time (the time between timer ticks), a significant load on the network. And the server has to handle a lot of garbage requests.

Therefore, a slightly improved mechanism, the so-called long-polling, is often used.
The difference is that the request is not sent every n seconds. He goes and hangs until he falls off or gets an answer. And when it falls off, a new request is sent.

Long polling is the most common way. Reinforced concrete, is used when good cross-browser compatibility is needed. The same gmail and vk uses it. But gradually it becomes morally obsolete.

2. Server-sent events.
This browser-based API allows you to receive events from the server via HTTP . It is used less often sockets and polling, because it is a kind of intermediate option. Polling is cross-browser, sockets are more convenient.

3. Web sockets.
A complete separate protocol for two-way client-server communication. That is, for such tasks specially created, and therefore much more convenient than HTTP . Over HTTP , only the first request occurs, handshake. New projects are usually written on sockets, because they are supported by all modern browsers, including ie from dozens and mobile.

  • висит пока не отвалится - and the server will not hang from a heap of requests? - Jean-Claude
  • @ Jean-Claude, of course, a server should support the ability to hold a bunch of such requests. But in most cases it is simpler than “send new n every second”, including from t. load. About the fact that this method is in principle ok - it already says that it has been used for some time almost everywhere. - Duck Learns to Take Cover