Glossary · Definition
WebSocket
A WebSocket is a persistent, bi-directional connection between a browser and a server. Unlike HTTP (request → response → close), a WebSocket stays open and either side can send messages anytime. Used for chat, collaborative editing, live dashboards, gaming, multiplayer.
Definition
A WebSocket is a persistent, bi-directional connection between a browser and a server. Unlike HTTP (request → response → close), a WebSocket stays open and either side can send messages anytime. Used for chat, collaborative editing, live dashboards, gaming, multiplayer.
What it means
The connection starts as an HTTP request with `Upgrade: websocket` headers. The server responds with HTTP 101 Switching Protocols, and from that point both sides speak the WebSocket frame protocol over the same TCP connection. Frames carry text or binary payloads, plus control frames (ping/pong for liveness, close for shutdown). The browser API is `new WebSocket(url)` with `onmessage`, `send(data)`, `close()` events. Production WebSockets need: heartbeat / ping (clean up dead connections), reconnection with backoff (network blips happen), authentication (typically pass a JWT in the connection URL or as the first message), and load-balancer support (sticky sessions — a WebSocket is a long-lived TCP connection to ONE server).
Advertisement
Why it matters
WebSockets are the right tool for: chat apps, live collaboration (Figma, Notion, Google Docs), live dashboards, multiplayer games, real-time market data, observability streaming. They're the WRONG tool for: simple notifications (use Server-Sent Events — simpler, unidirectional, automatic reconnect), most CRUD apps (HTTP is fine), and anything where the load balancer or CDN doesn't support sticky connections. Modern alternatives: Server-Sent Events (one-way streaming, simpler), HTTP/2 / HTTP/3 streams, and PubSub services (Pusher, Ably, AWS AppSync) that abstract the WebSocket plumbing.
Frequently asked questions
WebSocket vs Server-Sent Events?
SSE is one-way (server → client) and simpler — auto-reconnect built in, works over plain HTTP. WebSocket is bi-directional. If the client never needs to push to the server (only receive), SSE is the better choice.
WebSocket vs WebRTC?
WebRTC is peer-to-peer audio/video with a server only as a signaling mediator. WebSocket is client-server. Use WebRTC for voice/video calls; WebSocket for everything else.
Scaling WebSocket connections?
A single Node/Go/Elixir server handles 10k-100k connections per box, but you'll need load balancers with sticky sessions and a pubsub layer (Redis pub/sub, NATS, Kafka) so messages from one client reach others on different servers.
Related terms
- DefinitionAPIAn API (Application Programming Interface) is a contract that lets one program request something from another — typically over HTTP, in JSON. 'Web APIs', 'REST APIs', 'GraphQL APIs' are all flavors of the same idea: a defined surface for programmatic access.
- DefinitionHTTPSHTTPS is HTTP wrapped in a TLS-encrypted tunnel. Everything between your browser and the server — URLs, form data, cookies, response bodies — is encrypted in transit so a network observer (cafe Wi-Fi, ISP, anyone in between) can't read it or change it.