The chemistry of web sockets and speed
During the earlier years of the advent of the internet, new protocols, connection algorithms and theories were normal every day. Things that were state-of-the-art and exciting ground-breaking inventions like the I-phone are really boring in today’s fast-paced cloud market. But there is still something that has been relevant since the days of the early tube computers, SPEED.
Speed is as relevant today as it was a century ago. Everybody wants what they want, and now, they want it faster than ever.
User expectation on data loading has become de-facto, they want it real time and they want it now. And that is where the topic of this blog comes into play, web sockets.
But before jumping into that, let’s look at how your computer communicates with a server sitting in a cloud, and how sockets improve them.
Back in the 90s, the first hypertext transfer protocol (HTTP 1.0) was introduced, and it worked something like this.
HTTP 1.0
Each request had to open a separate connection to fetch the details from the server, which would then close, and another connection would be opened to fetch more data. This model, however, was not scalable and thus the need for HTTP 1.1.
HTTP 1.1
HTTP1.1 eliminated the need for opening a separate connection for each data file, and allowed multiple files to be fetched over the same persistent connection which would then close after the data transfer was completed.
It eliminated the extra overhead for opening a separate connection each time and allowed faster data fetching over the internet.
Although this model is still being used today, this isn’t what we are looking for. Imagine an application server that needs to send/inform the client about certain data without the client knowing or even asking for it. We will take an example to understand this.
If we are creating a chatting application like WhatsApp and we want to get the next message that our friend has sent us, we have two ways.
- The WhatsApp client application polls every n second to ask the server whether there is a message or not, and receives the data back
- The server informs the WhatsApp client app whenever a new message comes in.
We do not need to have a fancy engineering degree to understand which approach is the best and most efficient. The first approach is called polling where we ask for the data every n second and the second one is called pushing (not a commonly used term), which will be implemented with the help of web sockets.
A web socket connection
This is what a web socket connection looks like. Notice there is no “Close” connection request like HTTP. And the data is just flowing both ways continuously. Web sockets work in a full-duplex manner, that is, data transfers both sides and it transfers over a live connection which doesn’t terminate unless we terminate it manually through code or through the application itself. This completely eliminates the connect and disconnect overhead that we faced in HTTP and allows a tunnel to be formed between the server and the client where data can flow both ways continuously without any delay.
Applications of web sockets in real world are as follows
- Chatting applications like WhatsApp, Telegram, etc.
- Trading applications to get real time rates
- Gaming applications
- Any application based on data can be used to update the data in real-time without the need for manual refreshing.
Benefits of WebSocket
- Real-Time Updates: Web Sockets enable instant updates to be pushed from the server to the client, creating a seamless user experience.
- Reduced Latency: Since the connection remains open, there is no need to establish new connections for every interaction, leading to lower latency compared to traditional HTTP.
- Efficient Resource Usage: The elimination of the need to repeatedly open and close connections reduce the strain on servers and network resources.
- Bi-Directional Communication: Web Sockets allow both the client and server to send data independently, facilitating interactive applications.
- Scalability: Web Sockets are scalable and can handle a large number of concurrent connections, making them suitable for applications with high traffic.
While web-sockets are really amazing and super-fast, we do not need to use them in all applications. Though they must be used in gaming apps and chatting apps, the questions that software engineers should ask themselves before using web sockets in other kinds of applications is, “Do I need a full duplex connection?”, and “Is real-time data updating absolutely necessary for me?”.
If the answer to the above questions is Yes, then go ahead and use web sockets.
