How to update data in real time?

Asked

Viewed 999 times

9

Sites like Facebook, Twitter even this one we are now updating their data in real time, in the case of Facebook: when a user likes some post automatically the data on your screen is updated and so on, but I went to the Chrome network and realized that there are no "requests", as these updates are made?

  • 3

    If I’m not mistaken, it’s by Websockets

  • Yes, Websockets, the implementation is not so difficult, however it requires a few hours to understand where and how to apply it so that the optimization is well done.

1 answer

11

The name of this type of technology is Push (English Push), where the server initiates a transaction with the client (in your case, the browser).

It’s the opposite of what we normally use in web applications, where the action starts on the client side (Pull, from English Pull).

In this mechanism, the client state is changed at the request of the server. There are several ways to implement this behavior in modern browsers:

  • Traditional polling: Your application asks from time to time, via Xmlhttprequest or equivalent, if the server has any status update.
  • Long-Polling: Your application keeps an open parallel request that is only closed when the server sends some content.
  • Forever Frame: The same as Long-Polling, but without the overhead of creating/destroying the client’s control objects, and usually with a permanent connection.
  • Websockets: a relatively new protocol (2011) that operates in parallel to HTTP and optimizes bi-directional communication between client and server.

For more information, check the Wikipedia entry.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.