Why it matters

Every real-time web app needs WebSockets or an alternative (SSE, polling). WebSockets are the standard for bidirectional. Understanding them well shapes what your apps can do.

Advertisement

The architecture

Handshake: client sends HTTP GET with Upgrade: websocket header. Server responds with 101 Switching Protocols. TCP connection now speaks WebSocket protocol.

Frames: after handshake, both sides exchange framed messages. Frame types: text, binary, ping, pong, close.

WebSocket lifecycleHTTP upgradehandshakeWebSocket framestext / binary / controlPersistent TCPfull-duplexCompared to polling, cuts latency + server load dramatically for interactive apps
WebSocket protocol phases.
Advertisement

How it works end to end

Both sides can send anytime. No request/response coupling.

Text messages are UTF-8 strings; binary can be anything.

Ping/pong keeps connection alive through NATs and load balancers.

Subprotocols: negotiate at handshake (e.g., graphql-ws, wamp). Provides application-level protocol on top of WebSocket transport.