Networking

WebSocket

HTTP is one request, one response, then done — a client always has to ask before a server can say anything. WebSocket exists for the opposite need: a connection that stays open, over which either side can send a message at any moment, with no request required first.

Quick reference
What it is
A persistent, full-duplex connection over a single TCP connection, started via a normal HTTP handshake.
Mental model
A phone call instead of exchanging postcards — once connected, either side can speak whenever it wants, with no need to wait for a question first.
Key numbers
ws:// (plaintext, like port 80) or wss:// (encrypted, like port 443) — begins life as an ordinary HTTP request.
Reach for it when
A server needs to push data to a client the moment something changes, without the client repeatedly asking "anything new yet?"

Born from one HTTP request

A WebSocket connection starts as an ordinary HTTP GET request carrying two special headers — Upgrade: websocket and Connection: Upgrade — asking the server to repurpose this same TCP connection for something other than HTTP. If the server agrees, it replies with status 101 Switching Protocols (the one practical use of HTTP's rarely-seen 1xx status range), and from that exact point on, the connection stops speaking HTTP entirely and starts speaking the WebSocket framing protocol instead.

A client's HTTP request carrying Upgrade headers is answered with 101 Switching Protocols, after which the same TCP connection carries WebSocket frames independently in both directions Client Server GET /stream · Upgrade: websocket 101 Switching Protocols Same TCP connection — now carrying WebSocket frames, either direction, anytime client → server, anytime server → client, anytime
Only the handshake looks like HTTP — everything after "101 Switching Protocols" is a different wire format entirely.

Full-duplex — a genuine two-way street

Once upgraded, there's no more request/response pairing at all. The server can push a message the instant it has something new; the client can send one without prompting. Neither side is "waiting for a reply" the way HTTP structurally requires — both directions are simply open, independently, for as long as the connection lives.

WebSocket vs. polling

Before WebSocket, the only way to approximate "near real-time updates" over HTTP was polling — the client repeatedly sends a GET every few seconds, and the server answers "nothing new" almost every time. This wastes a full request/response cycle on every empty check, and caps how fresh the data can ever be at the polling interval. WebSocket removes both costs at once: no wasted empty requests, and updates arrive the instant the server has one to send, not up to N seconds later.

The cost: a stateful connection

HTTP's statelessness (see HTTP) is what lets any request be freely routed to any server behind a load balancer. A WebSocket connection breaks that assumption — once open, it's pinned to one specific server process for its entire lifetime, since the connection itself is the state. Scaling a WebSocket-heavy service means thinking about connection distribution and server affinity in a way a purely stateless HTTP API never has to.

Real-world applications

Gotchas

Field notes