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. The reason is our notorious “fast-paced world” and litany of 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 out that 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 of which we all are well familiar with:
-Video conference applications
-VoIP (voice over Internet Protocol)
-Community storage solutions
-Some e-commerce transactions
-IM (instant messaging)
The way an RTA functions depends mostly on realtime API mechanisms, of which there are four in total:
We’d like to tell you about WebSockets and polling as it represents the basic operating principle of an RTA.
That is how it functions:
1.There are certain time periods, after which a request is sent to the server to check for updates in the form of a vote;
2.The server on its side looks for any updates. If there are any, it confirms it by sending an up-vote. If not, it replies with a down-vote.
3.At the user end, if the server casts an up-vote, the system further asks for the updated data, which is then passed by the server.
4.Once this process is over, requests are sent again to the server at the regular time intervals.
At this, 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.
Where WebSockets is used
WebSockets is becoming increasingly more popular among Web developers nowadays, and can be implemented for the following purposes:
-Chat applications: Video and audio conferencing applications
-Multiplayer online gaming
-Simultaneous file sharing
The list can be continued further with:
-stock market monitoring
-an online broadcast, etc.
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
Your database 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.