WebSockets and the Architecture of the Real-Time Web
HTTP asks and waits. WebSockets keep a single connection open so the server can speak first — and that changes both what you can build and how you must secure it.
WebSocket is a full-duplex, bidirectional protocol for communication between a client and a server. Its URLs begin with ws:// or, when encrypted, wss://. Unlike HTTP, it is stateful: once the connection is established it stays open until one side closes it, and both parties can send messages at any moment over that same channel.
That single property — a connection that persists — is what makes live dashboards, multiplayer games and chat feel instantaneous. It is also what makes WebSockets behave very differently from the request-and-response model most web infrastructure was designed around.
How a WebSocket connection is made
Every WebSocket starts life as an ordinary HTTP request. The client asks the server to upgrade the connection; if the server agrees, the two complete a handshake and the underlying TCP connection is handed over to the WebSocket protocol. From then on, messages travel as lightweight frames in either direction until one side closes the channel.
Sequence diagram: the client sends an HTTP Upgrade request, the server replies 101 Switching Protocols, then messages flow in both directions over the open connection until a close frame ends it.
One HTTP upgrade, then a long-lived, two-way channel.
The browser API is deliberately small. A client opens a socket, listens for messages and sends its own whenever it needs to:
const socket = new WebSocket("wss://example.com/prices");
socket.addEventListener("open", () => {
socket.send(JSON.stringify({ type: "subscribe", symbol: "AVX" }));
});
socket.addEventListener("message", (event) => {
const update = JSON.parse(event.data); // treat server data as data
renderPrice(update);
});
socket.addEventListener("close", (event) => {
console.info("Socket closed", event.code, event.reason);
});
client.js
Where WebSockets earn their place
Real-time data
Trading platforms and live dashboards push price movements and metrics to the client the moment they change, over a connection that is already open.
Multiplayer and gaming
State arrives continuously and the interface updates without a refresh or a new connection for each change.
Chat and collaboration
A single connection handles sending, receiving, publishing and broadcasting messages between participants.
Field notes, deployment guides and research from the engineers who build web, cloud and mobile products at Aviox Blogs.
Insights & Understanding
Frequently Asked Questions
Answers to common questions regarding this article's topic, architecture, and practical applications.
WebSocket is a full-duplex, bidirectional protocol for communication between a client and a server. Its URLs begin with ws:// or, when encrypted, wss://. Unlike HTTP, it is stateful: once the connection is established it stays open until one side closes it, and both parties can send messages at any moment over that same channel.
Definition / Understanding
Real-time data Trading platforms and live dashboards push price movements and metrics to the client the moment they change, over a connection that is already open. Multiplayer and gaming State arrives continuously and the interface updates without a refresh or a new connection for each change. Chat and collaboration A single connection handles sending, receiving, publishing and broadcasting messages between participants. WebSo
General
Every WebSocket starts life as an ordinary HTTP request. The client asks the server to upgrade the connection; if the server agrees, the two complete a handshake and the underlying TCP connection is handed over to the WebSocket protocol. From then on, messages travel as lightweight frames in either direction until one side closes the channel. Sequence diagram: the client sends an HTTP Upgrade request, the server replies 101 Switching Protocols, t
General
Both protocols run over TCP, and teams often find it hard to choose between them. The difference is in who may speak, and for how long the line stays open.
General
WebSockets replace long polling, support duplex communication, carry binary as well as text data, and work across web, desktop and mobile clients. Once a connection is open, messages avoid the overhead of a new HTTP request each time. The costs are real too. Edge caching is impossible. You lose HTTP’s familiar status codes and semantics, so even a simple protocol of your own must define its own errors and acknowledgements. And if an application d