Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How WebSockets power chat, live scores and multiplayer apps: the HTTP upgrade handshake, frames, scaling, and WebSockets vs polling and SSE.
Quick answer: A WebSocket is a long-lived, two-way connection between a browser and a server. It starts life as an ordinary HTTP request asking to upgrade. The server agrees with a 101 Switching Protocols response, and from then on the same TCP connection carries small messages in both directions, whenever either side wants, with no new requests. That's what lets chat messages, live scores, stock prices and multiplayer moves appear instantly.
MDN's WebSocket API guide sums up the benefit: two-way messaging without polling the server for replies.
HTTP is a request-response protocol. The client asks, the server answers, and that's it. The server can't speak first. As the URL-to-page walkthrough shows, everything starts with the browser sending a request.
That's perfect for loading pages, and awkward for anything live. If a new chat message arrives on the server, how does it reach your browser? Before WebSockets, developers had three workarounds:
WebSockets, standardised in RFC 6455 in 2011, gave the web a proper two-way channel.

With long polling, every update and every reply costs a full HTTP request with headers. With a WebSocket, you pay for one handshake, then each message is a tiny frame that either side can send at any moment.
A WebSocket begins as a normal HTTP/1.1 request, which is why it passes through existing proxies, firewalls and load balancers. The browser asks to switch protocols:
GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: https://example.com
If the server supports WebSockets, it replies:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
The Sec-WebSocket-Accept value is derived from the client's random key by appending a fixed string and hashing it. It proves the server genuinely understood the WebSocket request rather than being a confused cache replaying an old response. (It is not a security feature; authentication is up to you.)
After the 101, HTTP is done. The underlying connection stays open, and both sides switch to the WebSocket protocol. On secure pages the URL uses wss://, which runs the whole thing inside TLS, exactly like HTTPS.
Data now flows as frames, each with a header of just a few bytes instead of hundreds for HTTP. Frame types include:
Frames sent from the browser are masked with a random key. That protects old network proxies from being tricked into caching attacker-controlled data.
In the browser, the built-in WebSocket API is all you need:
const socket = new WebSocket("wss://example.com/chat");
socket.addEventListener("open", () => {
socket.send(JSON.stringify({ type: "join", room: "general" }));
});
socket.addEventListener("message", (event) => {
const msg = JSON.parse(event.data);
console.log("New message:", msg);
});
socket.addEventListener("close", (event) => {
console.log("Closed with code", event.code);
// Reconnect here, with a backoff delay
});
On the server, a Node.js example using the popular ws package:
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
wss.on("connection", (ws) => {
ws.on("message", (data) => {
// Broadcast to every connected client
for (const client of wss.clients) {
if (client.readyState === client.OPEN) client.send(data.toString());
}
});
});
That's a working chat server in about ten lines. The hard part is running it for thousands of users.
| Short polling | Long polling | Server-Sent Events | WebSockets | |
|---|---|---|---|---|
| Direction | Client asks | Client asks, server waits | Server to client only | Both ways |
| Latency | Up to one poll interval | Low | Low | Lowest |
| Overhead per message | Full HTTP request | Full HTTP request | Small event in a stream | Tiny frame |
| Protocol | Plain HTTP | Plain HTTP | Plain HTTP, auto-reconnect built in | Upgrade to WebSocket |
| Binary data | Yes | Yes | Text only | Yes |
| Best for | Rare, non-urgent checks | Legacy fallbacks | Feeds, notifications, AI token streaming | Chat, collaboration, games, trading |
If data only flows from server to browser, SSE is often the simpler choice: it's plain HTTP, reconnects automatically, and works well over HTTP/2 and HTTP/3. Choose WebSockets when the client sends frequent messages too.
WebSockets run over TCP, so a lost packet delays everything behind it. For fast-paced games or media, the newer WebTransport API, which MDN notes may replace WebSockets for many uses, runs over QUIC and supports unreliable, unordered delivery. See TCP vs UDP for why that matters.
WebSockets are stateful: each client holds an open connection to one specific server for minutes or hours. That changes how you scale.
wss:// in production.Origin header during the handshake. Browsers send cookies with WebSocket requests, so without an origin check, a malicious site could open a socket as your logged-in user (cross-site WebSocket hijacking).Some CDNs can proxy WebSockets at the edge, giving you TLS termination and DDoS protection for real-time traffic too.
No. A WebSocket starts with an HTTP request, then switches to a different protocol on the same connection. After the 101 Switching Protocols response, no more HTTP is spoken on it.
TCP. That makes delivery reliable and ordered, but a lost packet delays later messages. WebTransport, built on QUIC over UDP, is the newer option for use cases that need unreliable or unordered delivery.
When updates only flow from server to client, such as notifications, live feeds or streaming AI responses. SSE is plain HTTP, reconnects automatically, and is simpler to scale.
It depends on memory, message volume and the server framework, but a well-tuned event-driven server can hold a very large number of mostly idle connections. Message fan-out and CPU per message usually become the limit before raw connection count.
Common causes are proxy or load balancer idle timeouts, mobile networks dropping idle connections, and server deploys. Send regular pings, raise idle timeouts, and build automatic reconnection into the client.
Socket.IO is a library that uses WebSockets when possible and adds features on top, like rooms, acknowledgements and fallbacks. A plain WebSocket client can't talk to a Socket.IO server without its own protocol.
WebSockets solve a simple problem with a simple trick: borrow an HTTP connection, upgrade it, and keep it open so either side can talk at any time. The protocol is easy; running it at scale is where the work is, from pub/sub fan-out to heartbeats and reconnection. When you need true two-way, low-latency messaging in the browser, it's still the default choice.