Web Sockets: Revolution in Real-Time Communication
Sending a message and getting an instant response without even refreshing the page is something we take for granted. But in the past, enabling real-time functionality was a real challenge for developers. Finally, the developers' community found a solution for building truly real-time apps: Web sockets. Before they came along, the “real-time” web existed, but it was difficult to achieve, typically slower, and was delivered by hacking existing web technologies that were not designed for real-time applications.
So, this article is all about building a deeper understanding of how WebSockets operate and what’s actually going on under the hood of applications using WebSockets.
![]() |
HOW WEB SOCKETS WORK?
WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time. (A quick review: the client is the application on a user’s computer, and the server is the remote computer that hosts the website and linked data).
![]() |
The client-server communication initially starts with an HTTP handshake. If both of the communicating parties(client and the server) agree to continue, a handshake is completed and the initial HTTP connection is replaced by a WebSocket connection. After this point, message exchange will take place in bidirectional mode until connection persists between client-server. Clients need not send requests again and again inquiring about new data. If anyone of them (client-server) dies or decides to close the connection is closed by both of the parties.
You might be wondering, the working of web sockets seems pretty simple. Then why is this a big deal?
So my dear friends, let's step back and look at the ways that we used before to fetch data from servers. This will help you to appreciate the power of web sockets.
Request-Response - In the traditional HTTP system, communication can only be initiated in one direction: from client to server.
Imagine a chat app where you’re talking to a friend. You send a message to the server, as a request with some text message. The server receives your request and stores the message. But, it has no way to reach out to your friend’s computer. Your friend’s computer also needs to send a request to check for new messages; only then can the server send over your message.
Aren't these delays between every message awkward?
Long Polling came as a solution to this problem. However, servers had to keep track of multiple requests and their order.
Here enter web sockets!
HTTP -> Long Polling -> Web Sockets
For seamless communication, web sockets it is.
FEW KILLER APPLICATIONS OF WEB SOCKETS
Stock Tickers: The finance world moves fast -- microsecond fast. When you have a dashboard tracking company you're interested in, you want to know what they're worth now, not 10 seconds ago. So, for displaying the price fluctuation and movement, data is continuously pushed by the backend server to the client end by using the web socket channel.
Chat apps: WhatsApp? This is the first thing that strikes my mind. But you might be familiar with this now. It's conceptually simple implementation of web sockets.
For multiplayer games, collaborative apps, and many more, WebSockets opens up a whole new world of possibilities.
Hopefully, by now you’re sold on Websockets!
Go Forth and use it :)
If you've any queries or feedback, feel free to post them in the comments section. Happy Coding :)
Great work Palak! This was very insightful.
ReplyDelete