WebSocket
A protocol that turns an HTTP connection into a long-lived, two-way channel where client and server can each send messages at any time.
In more detail
It starts as an HTTP request asking to upgrade. Afterwards messages need their own contract: event types, payload shapes, sequence numbers, size limits and close codes. Clients must reconnect with backoff, resume from a known position and re-authenticate, and both sides need backpressure.
Examples
Current Node.js has a global WebSocket client built in; a server still needs the upgrade event on node:http or a library such as ws.A chat subscription is a classic React Effect: open the connection in setup and close it in cleanup.Related terms
- HTTP upgrade — The exchange in which a client asks, with an Upgrade header, to switch an HTTP connection to another protocol, and the server agrees with 101 Switching Protocols.
- subprotocol — The application-level message format agreed on top of a WebSocket during the handshake, named in the Sec-WebSocket-Protocol header.
- heartbeat — A small message sent at regular intervals on a long-lived connection to prove that both ends and the path between them are still alive.
- close code — A number a WebSocket endpoint sends when closing a connection, telling the other side why, such as 1000 for normal closure.
- backpressure — A slow consumer signalling a fast producer to pause, so data does not pile up in memory faster than it can be handled.
- Server-Sent Events — A standard way for a server to keep an HTTP response open and push a stream of text events to the client, in one direction only.
- full duplex — Communication where both sides can send at the same time; one-way (unidirectional) communication only flows in one direction.
- HTTP — The protocol web browsers and web servers use to send requests and responses.
- frame — A self-contained unit of data in a protocol such as HTTP/2 or WebSocket, with a small header giving its type and length.
- useEffect — The React hook for running side work, such as adding listeners or changing document.body, after a component renders.
- cleanup function — The function an Effect returns so React can undo what the setup did, such as removing a listener or cancelling a request.
Taught in
- WebSockets — Modern API Architecture: REST, GraphQL & gRPC
- Server-Sent Events — Modern API Architecture: REST, GraphQL & gRPC
- The Same Servers For Less — AWS Certified Cloud Practitioner (CLF-C02) Exam Prep
- Billing Sprint: Worth An Evening? — AWS Certified Cloud Practitioner (CLF-C02) Exam Prep
- Serverless And Container Patterns: Where The Limits Bite — AWS Certified Solutions Architect – Associate (SAA-C03) Exam Prep
- Server-Sent Events: Streaming Updates & Recovery — Production Full-Stack Engineering
- Bidirectional WebSockets: Reconnection & Heartbeats — Production Full-Stack Engineering
- Streaming Endpoints: Server-Sent Events & NDJSON Feeds — Next.js 15 & Modern App Router Architecture
- Native HTTP/2 Protocols: Multiplexed Streams & Push — Node.js Internals & Backend Services
- Native WebSockets: Bidirectional Real-Time Streams — Node.js Internals & Backend Services
- useEffect Lifecycle: Mount, Dependencies, And Cleanup Phases — React 19 & Modern Component Architecture
References
- RFC 6455: The WebSocket Protocol
- MDN: The WebSocket API
- MDN: WebSocket
- RFC 6455: The WebSocket Protocol