▸Protocol notes for this page (click to expand)

WebSocket

What it is for

HTTP is directionally asymmetric: only the client sends requests and the server answers them, so the server cannot push a message to the browser on its own.

WebSocket completes a handshake with one HTTP request; after the 101 response that TCP connection carries frames in both directions, and either end may send at any time.

Relation to HTTP

WebSocket uses HTTP only for the handshake; after it completes, no HTTP message crosses the connection.

· The HTTP handshake lets it reuse ports 80/443 and pass through firewalls and proxies;
· After the 101 it uses its own frame format, with a header of only 2 to 14 bytes;
· WebSocket is not one of the HTTP versions: HTTP/1.1, 2 and 3 all follow the request-response model, and WebSocket is two-way.

The outer Ethernet, IP and TCP encapsulation is the same as for HTTP/2; what changes is the message format carried inside.

Common misconceptions

Only the handshake request of WebSocket is HTTP; after the handshake the framing differs from HTTP.

WebSocket is not a replacement for every HTTP request: it has no caching, no status codes and cannot be accelerated by a CDN. Ordinary data fetching should still use HTTP.

Production should use wss:// (that is, inside TLS): plain ws:// connections are often broken by proxies.