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.
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);
});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.
Keep reading — it’s free
The remaining 3 min of this story is for Dispatch subscribers. Subscribing takes one field, and the whole story opens right here.
Subscribe to read the rest
Free. One email when there’s something worth reading.
Originally published at Aviox Blogs engineering handbook.


