Skip to content
IT glossary
BackendUpdated December 3, 2026

WebSocket

This protocol lets a client and server send messages to each other over a connection that stays open. The server can push updates without waiting for the client to ask for them.

Also known as: вебсокет

What WebSocket is

Imagine a shared document. As you type, your browser sends edits to the server. At the same time, the server can send another person's edits to your browser without waiting for it to ask. WebSocket gives both sides a connection they can use independently. This is called full-duplex communication: a message from one side does not have to be a reply to a message from the other.

How the connection opens

With HTTP/1.1, the client starts with an HTTP request containing Upgrade: websocket. If the server accepts, it replies with 101 Switching Protocols. From then on, the same TCP connection carries WebSocket frames instead of HTTP requests and responses. HTTP/2 uses a different handshake based on extended CONNECT, not a 101 response.

WebSocket defines how to carry text and binary messages, not what they mean. A chat application might use JSON objects with a type field, while an application streaming sensor readings might use binary messages. Use wss:// for transport protected by TLS; ws:// is unencrypted.

A browser example

const socket = new WebSocket('wss://example.com/board');
socket.addEventListener('open', () => {
  socket.send(JSON.stringify({ type: 'subscribe', boardId: 42 }));
});
socket.addEventListener('message', ({ data }) => console.log(data));

The example assumes a server that understands subscribe; WebSocket itself does not define subscriptions. The browser waits for open before sending because constructing the object starts the handshake but does not complete it synchronously.

When to use it

WebSocket fits applications such as multiplayer games and collaborative editors, where both sides send frequent, time-sensitive messages. For a dashboard that mostly receives updates, Server-Sent Events (SSE) can provide the server-to-browser stream while ordinary HTTP handles user actions. If updates are infrequent and a delay is acceptable, periodic polling may be simpler.

An open connection is not a durable message queue. If a laptop goes offline, a replacement connection will not replay missed updates. A client can reconnect with increasing delays and then request a fresh snapshot or ask for events after the last one it processed. On the server, bound outgoing queues so a slow client cannot cause updates to pile up indefinitely.

What comes up in interviews

A common design question is how to deliver an update when the sender and recipient are connected to different backend instances. A load balancer routes the connection, but it does not distribute application events. One option is to publish events through a shared broker so the instance holding the recipient's connection can forward the update.