0% completed
What is WebSocket?
On This Page
The WebSocket Handshake
Life After the Handshake
Strengths of WebSockets
Weaknesses of WebSockets
Where WebSockets Are Used
WebSocket is a communication protocol that opens one connection between the client and the server and keeps it open. Once the connection is up, both sides can send messages at any time, in either direction. (A protocol is an agreed set of rules for how two computers talk to each other.)
Regular HTTP is like exchanging letters: you write, you wait for a reply, and you write again. A WebSocket is like a phone call: you dial once, the line stays open, and both people can speak whenever they want. This ability to talk in both directions at the same time is called full duplex.
The WebSocket Handshake
A WebSocket connection starts its life as a normal HTTP request. The client sends a regular request with a special header that says "I would like to upgrade this connection to WebSocket." If the server supports it, it replies with a special response confirming the switch. This exchange is called the WebSocket handshake.
After the handshake, the connection stops speaking HTTP. The same underlying TCP connection stays open and becomes a two-way message channel. (TCP is the reliable transport protocol we covered in TCP vs. UDP.)
Life After the Handshake
Once the channel is open:
- The client can send a message at any moment, without making a new request.
- The server can push a message at any moment, without being asked.
- Messages are small frames with very little overhead. There are no repeated headers and no new connections per message.
This is why WebSockets have the lowest latency of the three techniques in this chapter. The connection stays open until one side closes it.
Strengths of WebSockets
- True two-way communication. It is the only technique in this chapter where the client and the server are equal partners on one connection.
- Very low overhead per message. No new connection and no repeated headers for every message.
- Low latency. Messages flow instantly in both directions, which real-time apps like games depend on.
Weaknesses of WebSockets
- The server holds a connection per client. A million connected users means a million open connections to keep in memory.
- Scaling needs care. A WebSocket is stateful: your connection lives on one specific server. Load balancers must keep you pinned to that server, and spreading millions of long-lived connections across servers takes planning. (See Stateless vs. Stateful Load Balancing.)
- You handle reconnection yourself. If the connection drops, the application must detect it and reconnect. Browsers do not do this for you.
- Some old proxies and firewalls interfere. Long-lived upgraded connections are not plain HTTP, and strict middleboxes sometimes cut them off.
Where WebSockets Are Used
WebSockets are the default choice when both sides need to talk:
- Chat applications: messages travel both ways, instantly.
- Multiplayer games: every player action must reach the server and other players with minimal delay.
- Collaborative editing: several people typing into the same document.
- Live dashboards and trading: prices stream down while orders go up.
If your data only needs to flow one way, from the server down to the client, there is a simpler option. That is the topic of the next lesson: What are Server-Sent Events?
On This Page
The WebSocket Handshake
Life After the Handshake
Strengths of WebSockets
Weaknesses of WebSockets
Where WebSockets Are Used