Start building a real-time application
Real-time is a buzzword that we hear across now and then, where data is updated as soon as data is updated, This article helps to kick start your real-time data sync application. Before diving deep into the details let us see few examples where it is widely used.
- Stock app — Updating prices as per current market trends.
- News section — Updating current events of the day.
- Chat application — Updating mutual chat conversions.
- A live dashboard wall fed by Twitter’s streaming API.
- A monitor for server statistics like uptime, health, and running processes.
All the above examples require data to be shared across devices as soon as the event happens. This syncing of data from server to client can be done broadly via 2 ways:
1. Client pull: It’s the process of pulling updated data from the server via HTTP methods at regular intervals of time. Further, it can be classified into short polling and long polling. Short polling is an AJAX-based timer that calls at fixed delays whereas Long polling is based on Comet (i.e server will send data to the client when the server event happens with no delay).
With this method, you can receive the latest data, which is near real-time because of the delay time between the requests. With this method, the frontend has no idea when the data is updated, so the data is updated at the next request till then it will be showing the old data. Also, this method there is a trade-off with increase server load and client bandwidth as it’s been called frequently to be in sync with live data. Hence we can say this method is not efficient as server push, where data is updated whenever there is a change in data.
2. Server push: It’s also called as publish/subscribe model, where server publish an event and clients subscribe to the events to receive updates via web socket connections. These are long-lived connections that are created between the client and the server that acts as a medium for data sharing whenever a change is triggered in the server.
In the next article, you can find the detailed implementation of server push concept using socket.io.
References:
- Article of Nathan peck
- “Polling vs SSE vs WebSocket” by codeburst