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.
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.