ELI5: the benefits of WebSockets
Smooth, fast, seamless communication is now a digital standard. Whether it’s an online game, a live chat session, an IoT sequence, or a collaborative document – we expect web services to process data back and forth in real-time.
And WebSockets make this possible. Though we’re all increasingly used to it, our systems sending and receiving data without delay is no small technological feat. WebSockets, then, are a linchpin of our digital lives.
Yet for the non-technical, it’s not exactly clear what they are, or why they matter. So, here’s an overview of the benefits of WebSockets. (With bonus insights on how they work.)
What are WebSockets, exactly?
WebSockets are a digital communications protocol. That is, they’re a method for clients to establish a connection and communicate with a server. Let’s break down that terminology.
WebSockets allow you to open a continuous connection between client and server. So, after the initial connection, the two can communicate until the connection is actively closed.
This is in comparison to HTTP, which is a request/response model. With HTTP, connections open and close with each data packet sent. It’s a bit like ordering food through a waiter. The waiter takes your order to the kitchen, the kitchen then has to send the waiter back. And the only way you can get information from the kitchen is by asking the waiter.
A WebSocket, meanwhile, creates a faster, smoother flow. It’s a bit like plugging a client into the server — the connection is there until you pull the plug out again.
The benefits of WebSockets
So, we know a little about how WebSockets work at the layman’s level. But why do they matter? Or rather, what are their specific advantages?
We can break this into 4 easy to understand areas:
- Connections – i.e. how clients and servers communicate
- Latency – i.e. delay
- Flexibility – i.e. how data is shared
- Security – i.e. how data is protected in transit
Let’s take a closer look at each of these benefits of WebSockets.
Connections using WebSockets are bidirectional — they allow ‘full-duplex communication’. This means that, once the initial handshake has taken place and the connection is established, the client and server can both communicate with each other freely. (And at the same time.)
There’s no need to make requests – data can be sent to the client at any time. Without, that is, the client requesting it. This is known as a ‘server push’.
In other words, both the client and the server can stream messages to each other as needed. WebSockets, then, facilitate real-time communication. There is no sending extra requests just to check for messages or updates.
Another benefit of WebSockets is that they have low latency. This means that there’s very little delay between the time you request something and the response.
This is because, thanks to the continuous connection, all the messages after the initial handshake contain only relevant information.
So, compared to HTTP, data transfer is smaller and can happen much faster. There’s none of the usual HTTPS overhead that would normally need to be loaded every time. (Overhead is the extra information and data that must go along with a payload to ensure it’s effectively delivered. For instance, HTTP header, cookies etc.)
The WebSocket protocol is also flexible. It supports all sorts of subprotocols and extensions. This means that WebSockets can support additional functionality. Formats for messages can be outlined using agreed-upon subprotocols.
These subprotocols are agreed upon during the initial handshake, when establishing the connection between client and server.
The caveat here is to make sure you’re using a WSS (WebSocket secure) connection, rather than a WS (WebSocket) connection. This is like using HTTPS instead of HTTP.
What they’re useful for
So, where are the benefits of WebSockets most useful? Here are a few examples:
- Browser-based, multiplayer gaming
In online multiplayer games, reaction times are often key. The low latency afforded by WebSocket connections, then, makes WebSockets beneficial to online gamers.
- Chat/messaging apps
Due to the real-time communications and as-needed data sending, WebSockets are great for chat applications. Instead of polling – constantly hitting the server to check for new messages — replies are sent without the client needing to request them.
- The Internet of Things (IoT) applications
WebSockets can streamline the fast exchange of information between IoT devices.
WebSockets make our software run quicker, our websites react in real-time, our data flow as needed.
But for all the benefits of WebSockets, they aren’t a replacement for HTTP or other communication protocols. Rather, it’s another option. If your system needs real-time updates, back and forth data exchanges and low latency, WebSockets are great. If it’s something like a one-directional news feed, though, they might be overkill.
Either way, as the web gets faster, as software systems evolve and integrate, WebSockets are an integral part of online communications.