The WebSocket Upgrade Handshake
HTTP is request-driven; a client must request data before the server responds. WebSockets break this constraint by upgrading an existing HTTP connection into a stateful, bidirectional TCP stream.
The client initiates this by sending an HTTP request containing the header "Upgrade: websocket". If accepted, the server returns status 101, and the TCP socket remains open for low-overhead message frames.
GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13Full-Duplex Communication
Once upgraded, either the client or the server can push raw text or binary data frames at any time without repeating cookies, user-agents, or headers. WebSockets are optimal for chats, financial tickers, and live telemetry panels.
