Actual-time communication is in all places — stay chatbots, knowledge streams, or immediate messaging. WebSockets are a robust enabler of this, however when must you use them? How do they work, and the way do they differ from conventional HTTP requests?
This text was impressed by a latest system design interview — “design an actual time messaging app” — the place I stumbled by some ideas. Now that I’ve dug deeper, I’d wish to share what I’ve realized so you may keep away from the identical errors.
On this article, we’ll discover how WebSockets match into the larger image of shopper‑server communication. We’ll talk about what they do properly, the place they fall brief, and — sure — tips on how to design an actual‑time messaging app.
At its core, client-server communication is the trade of knowledge between two entities: a shopper and a server.
The shopper requests for knowledge, and the server processes these requests and returns a response. These roles are usually not unique — companies can act as each a shopper and a server concurrently, relying on the context.
Earlier than diving into the main points of WebSockets, let’s take a step again and discover the larger image of client-server communication strategies.
1. Brief polling
Brief polling is the best, most acquainted strategy.
The shopper repeatedly sends HTTP requests to the server at common intervals (e.g., each few seconds) to verify for brand spanking new knowledge. Every request is impartial and one-directional (shopper → server).
This methodology is simple to arrange however can waste sources if the server not often has contemporary knowledge. Use it for much less time‑delicate functions the place occasional polling is ample.
2. Lengthy polling
Lengthy polling is an enchancment over brief polling, designed to cut back the variety of pointless requests. As an alternative of the server instantly responding to a shopper request, the server retains the connection open till new knowledge is accessible. As soon as the server has knowledge, it sends the response, and the shopper instantly establishes a brand new connection.
Lengthy polling can also be stateless and one-directional (shopper → server).
A typical instance is a trip‑hailing app, the place the shopper waits for a match or reserving replace.
3. Webhooks
Webhooks flip the script by making the server the initiator. The server sends HTTP POST requests to a client-defined endpoint every time particular occasions happen.
Every request is impartial and doesn’t depend on a persistent connection. Webhooks are additionally one-directional (server to shopper).
Webhooks are extensively used for asynchronous notifications, particularly when integrating with third-party companies. For instance, fee techniques use webhooks to inform purchasers when the standing of a transaction adjustments.
4. Server-Despatched Occasions (SSE)
SSEs are a native HTTP-based occasion streaming protocol that enables servers to push real-time updates to purchasers over a single, persistent connection.
SSE works utilizing the EventSource
API, making it easy to implement in trendy internet functions. It’s one-directional (server to shopper) and preferrred for conditions the place the shopper solely must obtain updates.
SSE is well-suited for functions like buying and selling platforms or stay sports activities updates, the place the server pushes knowledge like inventory costs or scores in actual time. The shopper doesn’t have to ship knowledge again to the server in these situations.
However what about two-way communication?
All of the strategies above give attention to one‑directional circulation. For true two‑method, actual‑time exchanges, we’d like a special strategy. That’s the place WebSockets shine.
Let’s dive in.
WebSockets allow real-time, bidirectional communication, making them good for functions like chat apps, stay notifications, and on-line gaming. Not like the standard HTTP request-response mannequin, WebSockets create a persistent connection, the place each shopper and server can ship messages independently with out ready for a request.
The connection begins as an everyday HTTP request and is upgraded to a WebSocket connection by a handshake.
As soon as established, it makes use of a single TCP connection, working on the identical ports as HTTP (80 and 443). Messages despatched over WebSockets are small and light-weight, making them environment friendly for low-latency, high-interactivity use instances.
WebSocket connections observe a selected URI format: ws://
for normal connections and wss://
for safe, encrypted connections.
What’s a handshake?
A handshake is the method of initialising a connection between two techniques. For WebSockets, it begins with an HTTP GET request from the shopper, asking for a protocol improve. This ensures compatibility with HTTP infrastructure earlier than transitioning to a persistent WebSocket connection.
- Shopper sends a request, with headers that seem like:
GET /chat HTTP/1.1
Host: server.instance.com
Improve: websocket
Connection: Improve
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://instance.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Model: 13
Improve
— alerts the request to modify the protocolSec-WebSocket-Key
— Randomly generated, base64 encoded string used for handshake verificationSec-WebSocket-Protocol
(elective) — Lists subprotocols the shopper helps, permitting the server to choose one.
2. Server responds to resquest
If the server helps WebSockets and agrees to the improve, it responds with a 101 Switching Protocols standing. Instance headers:
HTTP/1.1 101 Switching Protocols
Improve: websocket
Connection: Improve
Sec-WebSocket-Settle for: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Settle for
— Base64 encoded hash of the shopper’sSec-WebSocket-Key
and a GUID. This ensures the handshake is safe and legitimate.
3. Handshake validation
With the 101 Switching Protocols
response, the WebSocket connection is efficiently established and each shopper and server can begin exchanging messages in actual time.
This connection will stay open until it’s explicitly closed by both celebration.
If any code apart from 101
is returned, the shopper has to finish the connection and the WebSocket handshake will fail.
Right here’s a abstract.
We’ve talked about how WebSockets allow real-time, bidirectional communication, however that’s nonetheless fairly summary time period. Let’s nail down some actual examples.
WebSockets are extensively utilized in real-time collaboration instruments and chat functions, corresponding to Excalidraw, Telegram, WhatsApp, Google Docs, Google Maps and the stay chat part throughout a YouTube or TikTok stay stream.
1. Having a fallback technique if connections are terminated
WebSockets don’t mechanically get well if the connection is terminated as a result of community points, server crashes, or different failures. The shopper should explicitly detect the disconnection and try to re-establish the connection.
Lengthy polling is commonly used as a backup whereas a WebSocket connection tries to get reestablished.
2. Not optimised for streaming audio and video knowledge
WebSocket messages are designed for sending small, structured messages. To stream massive media knowledge, a expertise like WebRTC is best suited to these situations.
3. WebSockets are stateful, therefore horizontally scaling is just not trivial
WebSockets are stateful, which means the server should preserve an energetic connection for each shopper. This makes horizontal scaling extra advanced in comparison with stateless HTTP, the place any server can deal with a shopper request with out sustaining persistent state.
You’ll want a further layer of pub/sub mechanisms to do that.
Now let’s see how that is utilized in system design. I’ve lined each the straightforward (unscalable) answer and a horizontally scaled one.