← Lessons

quiz vs the machine

Gold1400

Networking

The WebSocket Upgrade Handshake

How an HTTP request becomes a persistent two way socket.

5 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What status code confirms a WebSocket upgrade?

2. Why does WebSocket start as an HTTP request?