From Request to Socket
WebSocket gives the browser a persistent, full duplex connection. It starts life as an ordinary HTTP request so it can reuse the same port and pass through proxies, then upgrades the connection into a raw message channel.
The Handshake
- The client sends a GET request with Upgrade websocket and Connection Upgrade headers.
- It includes a random Sec WebSocket Key to prove this is a real handshake.
- The server replies with status 101 Switching Protocols and a derived Sec WebSocket Accept value.
- After that the TCP connection no longer speaks HTTP; both sides exchange WebSocket frames.
After the Upgrade
Messages are carried in lightweight frames in either direction at any time, with no per message request overhead. This suits chat, live dashboards, and games where the server must push instantly.
Key idea
A WebSocket begins as an HTTP request with an Upgrade header, and a 101 response switches the same connection into a persistent full duplex frame channel.