The era of WebSockets, as well as real-time applications (RTA), is upon us. Persistent connections and broadcasting to connected clients is now part and parcel of almost every modern application. Today’s consumers expect on-demand apps. So, how do we take part by building an RTA?
If we open the Wikipedia article devoted to the real-time web, we’ll find the following description of an RTA:
“An RTA enables users to receive information as soon as it is published by its authors, rather than requiring that they or their software check a source periodically for updates.” This means that all operations are happening with a small latency (usually measured in seconds) that allows users to think that everything happens in real-time. In other words, all processes are felt as immediate and current.
Here are some examples of RTAs most will be familiar with:
The way an RTA functions depends mostly on realtime API mechanisms, of which there are four in total:
Here we’d like to explain a little more about WebSockets and polling as they represent the basic operating principle of an RTA.
Here is how polling functions:
Ajax is chosen because of being lightweight and able to update parts of the page without reloading it entirely. The only issue is with the bandwidth, which is later resolved with the help of a method called long polling. According to the method, an HTTP request stays up until a new message is sent by a server.
Long polling via Ajax
Today, these approaches might be still used, yet most developers tend to use other ways of achieving server-push.
If we began to talk about polling and long polling, we can’t omit such things as Comet. This is basically a neologism that describes the model of a web application. In the usual cycle of request processing, the client initiates the connection and requests the document of interest. After the data is returned, the connection is broken. In the case of Сomet, the server initiates the connection and sends a message to the client. A good example of Comet is notifications in Facebook, Twitter, chat in Gmail, etc.
The reason it’s mentioned in conjunction with polling is that Comet includes and is being realized through polling, long-polling, and even HTML5 WebSockets, which we’ll discuss further.
Back in 2011, the WebSockets protocol appeared. Its primary function is to provide a bi-directional communication channel. This allows a browser and a web server to interact, providing them with a two-way persistent connection, within which they can send messages back and forth while the connection is open.
The WebSockets protocol is immensely important for the responsiveness of an application, which usually has many concurrent connections and quickly changing content — a good example is a multiplayer online game.
WebSockets is becoming increasingly more popular among Web developers nowadays, and can be implemented for the following purposes:
The list can be continued further with:
After we have got some insight into the topic, let’s define what technology pool we need for the development of an RTA on the base of WebSockets API mechanism.
Your platform is
The K&C team hopes you find this article useful as your application enters the world of real-time! If you still have questions, don’t hesitate to reach out to us.