⚡ WebSockets & Real-time
Beginner
What is the WebSocket handshake?
Answer
The WebSocket connection begins with an HTTP Upgrade handshake. The client sends a standard HTTP GET request with special headers: Upgrade: websocket, Connection: Upgrade, and a Sec-WebSocket-Key (a base64-encoded random value). The server responds with HTTP status 101 Switching Protocols, echoes back a Sec-WebSocket-Accept header (derived by hashing the key with a GUID), and the connection is upgraded from HTTP to the WebSocket protocol. After this handshake, the TCP connection remains open and both sides communicate using WebSocket frames instead of HTTP messages. The handshake happens over the same port as HTTP (80) or HTTPS (443), which helps with firewall compatibility.