Bookmarks

WebSocket protocol

A network protocol for two-way communication over a single TCP connection: client and server open a channel and exchange messages in both directions, without repeated HTTP requests.

Also: WebSocket, WebSocket connection, two-way channel

checked 9 Nov

All terms

Plain HTTP works as “request and response”: the client asks, the server answers, and the server cannot send data on its own. For chats, games and live quotes this had to be worked around by polling the server and opening a separate connection for every message. WebSocket, defined in RFC 6455 (December 2011), solves the problem with one persistent channel.

How a connection is opened

It all starts with an ordinary HTTP GET request with the Upgrade and Connection headers: the client asks to switch protocols. The server replies with status 101 and a Sec-WebSocket-Accept value computed from Sec-WebSocket-Key, so that the client can be sure it is talking to a real WebSocket server. After that both sides use the same TCP connection directly, and HTTP no longer takes part in the exchange. The URL schemes are ws:// (port 80 by default) and wss:// (port 443, the same protocol inside TLS). In the browser, the WebSocket constructor, an interface from the WHATWG standard, handles the connection.

http
            GET /chat HTTP/1.1            Host: server.example.com            Upgrade: websocket            Connection: Upgrade            Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==            Sec-WebSocket-Version: 13             HTTP/1.1 101 Switching Protocols            Upgrade: websocket            Connection: Upgrade            Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=            

Frames, ping and closing

Data travels in frames: text (opcode 0x1) or binary data (0x2); the protocol can split a large message into several frames. The control frames are close, ping and pong: either side may send a ping, and the other must reply with a pong carrying the same content as soon as possible, which is how liveness is checked. Closing is also polite: one side sends a close frame with a reason code, for example 1000 for a normal closure, and the other side acknowledges it.

FrameOpcodePurpose
Text0x1a message encoded as UTF-8
Binary0x2arbitrary bytes
Close0x8closes the connection with a reason code
Ping0x9checks that the connection is alive
Pong0xAreply to a ping with the same content

Where it is used

RFC 6455 names typical cases: games, stock tickers, collaborative editing, and real-time interfaces to services; in practice the list is longer and includes chats and messengers. Every open connection takes up server memory and file descriptors, so their number is limited. Passing WebSocket through a reverse proxy takes separate settings, because ordinary proxying is designed for “request and response”. If the server only needs to push events down to the client, a simpler mechanism is often enough: Server-Sent Events over plain HTTP.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • webhook

    A webhook is a one-off HTTP request triggered by an event, while WebSocket is a persistent connection over which both sides send messages at any time.

In depth in the references

where the topic is covered in detail