The opening handshake

A WebSocket connection starts as an HTTP/1.1 request with an Upgrade header. The client offers a random Sec-WebSocket-Key; the server returns its hash (SHA-1 + magic GUID) in Sec-WebSocket-Accept.

GET /chat HTTP/1.1
Host: 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=

Frame format

After the handshake, both sides exchange frames:

Control frames

WebSockets are not HTTP/2 streams. Browsers still open WebSocket connections over HTTP/1.1. WebSockets over HTTP/2 (RFC 8441) requires explicit server opt-in via the SETTINGS_ENABLE_CONNECT_PROTOCOL setting.

Common use cases