WebSockets explained in plain terms means a way for a web browser and a server to keep one connection open and talk to each other in both directions, instantly, instead of the browser asking again and again. The central question is how a live chat message or a stock price can appear on your screen the moment it changes, and the short answer is that WebSockets replace repeated requests with a single lasting conversation, so either side can speak whenever it has something to say.
Think of the difference between sending letters and making a phone call. With letters, you write, wait for a reply, and write again. With a phone call, the line stays open and both people can speak at any moment. Ordinary web requests are the letters; WebSockets are the phone call.
You use WebSockets constantly without noticing. Live chat, collaborative documents, online games, trading screens, and sports scores all rely on them to push updates to your screen the instant they happen, rather than making your browser ask for them every few seconds.
Key takeaways
- WebSockets keep one connection open for two-way, real-time communication.
- Either side can send a message at any time, without waiting to be asked.
- They start as an ordinary web request, then upgrade to a lasting connection.
- They power chat, live scores, trading screens, and collaborative tools.
- They suit live updates, not one-off requests or file downloads.
What WebSockets are
WebSockets are a protocol, standardised in 2011, that provides full-duplex communication over a single connection between a client, usually a browser, and a server. Full-duplex simply means both sides can send messages independently, at any time, without taking turns or waiting for a request.
An ordinary web connection works differently. The browser asks for a page, the server answers, and the connection closes. To get fresh information, the browser must ask again, and again, a pattern called polling. Polling works, but it wastes effort: most checks return nothing new, and updates still arrive late, limited by how often the browser asks.
A WebSocket connection begins as an ordinary web request, with a special header asking to upgrade. If the server agrees, the connection switches to the WebSocket protocol and stays open. From then on, messages flow as small frames in either direction, with very little overhead per message, which makes the connection cheap to keep alive and fast to use.
Our guide to API design explains the request-and-response pattern WebSockets complement. APIs answer questions when asked; WebSockets deliver news the moment it happens.
How WebSockets work
Opening a WebSocket starts with a handshake. The browser sends a normal web request to the server, including an upgrade header and a security key. The server replies that it accepts the upgrade, and the connection changes from the request-and-response pattern to a persistent, two-way channel. The whole switch takes a fraction of a second.
Once open, messages travel as frames: small packages with a tiny header and a payload of text or binary data. Either side can send a frame at any moment. A chat server can push a new message to every connected browser the instant it arrives, and a browser can send a keystroke or a cursor movement without waiting for a reply.
Because the connection stays open, the server knows who is connected and can send updates only to those who need them. A trading screen subscribes to the prices it shows, a document editor subscribes to the document being edited, and a game server sends each player only what their screen needs.
Connections can break, of course, and good applications handle that. The client usually reconnects automatically, re-subscribes to its updates, and asks the server for anything it missed while away. Heartbeat messages, small pings sent at intervals, let both sides notice a dead connection quickly and clean it up.

Comparing WebSockets with polling
| Aspect | WebSockets | Polling an API |
|---|---|---|
| How updates arrive | Pushed instantly by the server | Fetched when the client asks |
| Delay | Near instant | Up to the polling interval |
| Network load | Low, messages only when needed | High, many empty checks |
| Server load | One open connection per client | Many short requests |
| Best for | Live chat, games, trading, collaboration | Occasional updates and simple apps |
Where WebSockets are used
Live chat is the classic example. When you send a message, the server pushes it to the other person’s screen immediately, and typing indicators, read receipts, and presence information all travel the same way. Without WebSockets, chat would feel sluggish, with messages arriving only when the app happened to check.
Collaborative tools depend on them too. When several people edit the same document, each keystroke travels to the server and out to the other editors in milliseconds, so everyone sees the same text. The same pattern drives shared whiteboards, project boards, and live design tools.
Trading and sports screens use WebSockets to stream prices and scores as they change, and online games use them for player positions and events. Our guide to API gateways explains how services organise and protect the request-based calls that most apps still use for everything that is not live, from loading a profile to submitting a form.
Notifications and dashboards are another common use. A monitoring dashboard that shows server health, a delivery tracker that follows a parcel, or a news site that updates a live blog all use WebSockets to push new information the moment it exists, instead of asking the server to check again every few seconds.
Strengths and limits
WebSockets’ greatest strength is immediacy with low overhead. Once the connection is open, sending a message costs almost nothing, so updates arrive instantly without the waste of constant checking. For anything live, the experience is noticeably better than polling.
They also simplify the server’s job in one respect: the server knows exactly who is connected and what each client cares about, so it can send targeted updates instead of answering the same question from thousands of clients every few seconds.
But open connections cost resources. A server holding a hundred thousand connections must manage them all, keep them alive, and notice when they die. Firewalls and proxies sometimes interfere with long-lived connections, and older infrastructure may close them unexpectedly, so applications need reconnection logic.
WebSockets are also the wrong tool for one-off work. Loading a page, fetching a file, or submitting a form is simpler and more reliable as an ordinary request. They shine where data changes constantly and both sides need to know at once, and they add complexity everywhere else.
WebSockets checklist
- Use WebSockets for live updates, not for one-off requests.
- Reconnect automatically and re-subscribe after a dropped connection.
- Send heartbeat messages so dead connections are noticed quickly.
- Authenticate the connection during the handshake, not afterwards.
- Use encrypted connections, so messages cannot be read or changed.
- Limit message size and rate, to stop one client flooding the server.
- Plan for scale, since each client holds a connection open.
- Ask the server for missed updates after reconnecting.
- Test through real firewalls and proxies, which may close idle connections.
Frequently asked questions
What are WebSockets?
WebSockets are a protocol that keeps a single connection open between a browser and a server, allowing messages to flow in both directions at any time. They are used for live updates such as chat, scores, trading prices, and collaborative editing.
How are WebSockets different from HTTP?
HTTP is request and response: the client asks, the server answers, and the connection closes. WebSockets start as an HTTP request, then upgrade to a lasting two-way connection, so either side can send a message whenever it has one.
What is the WebSocket handshake?
The handshake is the opening exchange, in which the browser sends a normal web request asking to upgrade, and the server agrees. Once the handshake completes, the connection switches to the WebSocket protocol and stays open.
Do WebSockets work with encryption?
Yes. Secure WebSockets run over an encrypted connection, just as secure web pages do, and they should always be used when messages carry anything sensitive or when the site itself is served securely.
What happens if a WebSocket connection drops?
The client usually reconnects automatically, re-subscribes to the updates it needs, and asks the server for anything it missed. Good applications treat disconnection as normal and recover from it gracefully.
When should I not use WebSockets?
For one-off requests, file downloads, or anything that does not need live updates. Ordinary web requests are simpler, easier to cache, and better supported by standard infrastructure for those jobs.
The bottom line
WebSockets explained in one line is this: a single open phone line between browser and server, so either side can speak the moment it has something to say.
They make chat instant, collaboration smooth, and live screens genuinely live, at the cost of managing many open connections. Use them where updates must arrive the moment they happen, and stick to ordinary requests for everything else.
This article is general information about how WebSockets work, not advice about which real-time technology suits a particular application.







