What is a WebSocket and when do you really need one

Published on 2026-09-12 by frfrand

An open connection in both directions: how it starts as HTTP and stops being one, what it costs to maintain it, and why something simpler is often enough.

What is a WebSocket and when do you really need one

Short answer: a WebSocket is a connection that starts as an HTTP request, is "upgraded" with the 101 response, and stays open so client and server can send messages whenever they want. It's useful when the server needs to speak first and repeatedly (chat, collaborative editing, live data); if the client only receives, Server-Sent Events or periodic polling are usually enough.

HTTP has a property that defines almost all web development: the client asks and the server responds. The server never speaks first.

For a content website that's perfect. For a chat, a live board, or collaborative editing, it's a problem: the server knows something new and has no way to notify you.

WebSockets solve exactly that.

How it starts

The interesting thing is that it starts as a normal HTTP request, with a special header:

GET /ws HTTP/1.1 Upgrade: websocket Connection: Upgrade

If the server accepts, it responds with an uncommon code:

HTTP/1.1 101 Switching Protocols

From that moment on that TCP connection stops speaking HTTP. It stays open, and both sides can send messages whenever they want, without asking permission.

That design (starting as HTTP and then changing) is not a whim: it's what allows it to work through proxies and firewalls that only expect web traffic. It uses the same ports 80 and 443.

What it costs

One open connection per client. A thousand connected users are a thousand live connections, each occupying memory and a file descriptor. It's manageable, but you have to size it on purpose: default open descriptor limits are reached sooner than you expect.

State is no longer trivial. With HTTP you can put ten servers behind a load balancer and each request goes to any of them. With WebSockets, the client is tied to a specific server. If you want a message to reach a user connected to another server, you need something in the middle to distribute it (typically Redis).

Reconnection is yours. Connections drop: switching from Wi-Fi to mobile data, the phone goes to sleep, a proxy cuts due to inactivity. The browser does not reconnect on its own. You have to implement retries with increasing wait times and, above all, recover what happened while you were disconnected. That second point is the most forgotten and the one that produces hard-to-find bugs.

Intermediaries cut connections. Many proxies close inactive connections after one or five minutes. That's why a periodic ping is sent, not by the protocol but so no one thinks it's dead.

When it's worth it and when it's not

It's worth it when the server has to speak first, followed: chat, instant notifications, live quotes, collaborative editing, games, panels that update themselves.

It's not worth it when the event is infrequent or when the client can ask without cost. Keeping a thousand open connections to report something that happens three times a day is a waste, including maintenance.

The forgotten alternatives

Polling every so often. It sounds primitive and is often the right answer. If the data changes every few minutes and the application doesn't need to react instantly, periodic polling is infinitely simpler to operate.

Server-Sent Events. An open connection where only the server sends, over normal HTTP. The browser reconnects on its own, which eliminates the most annoying part. If you only need to receive (notifications, task progress, a panel that updates), this is usually better than a WebSocket and almost no one considers it.

The question that orders the decision: do I need to send data from the client to the server through that same channel, and very often? If the answer is no, you probably don't need a WebSocket.

A note on the proxy

If you put nginx in front, you need to configure it so it doesn't break the protocol upgrade:

location /ws { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 3600s; }

Without the Upgrade and Connection headers, the initial handshake does not reach the backend properly and the connection is not even established. Without proxy_read_timeout, it is established, but nginx cuts it after 60 seconds without traffic (its default value): one of the most frustrating errors to diagnose because "it works locally".

A WebSocket in production: what to review

A chat application that uses WebSocket needs to keep active connections; loading a static page is not enough. Check that the reverse proxy supports the upgrade protocol, that the API authenticates the connection, and that the server can handle the expected number of clients.

On an ArduMaker VPS you can run the WebSocket backend behind Nginx and associate it with a domain with HTTPS. The edge proxy that terminates HTTPS already lets WebSocket through, but cuts connections that pass 360 seconds without traffic, even if your internal Nginx has a longer time: send a ping every less time. If you are going to distribute traffic between several instances, you also need to design how they share events and state: a load balancer does not solve synchronization by itself.

Related: WebSocket, webhook or polling · Nginx explained.

Frequently asked questions

What is the difference between WebSocket and HTTP? HTTP is request and response: the server never speaks first. A WebSocket starts as HTTP and, after code 101, remains as an open channel in both directions.

WebSocket or Server-Sent Events? If the client only receives (notifications, progress, dashboards), SSE is usually better: it runs over normal HTTP and the browser reconnects on its own. WebSocket is justified when the client also sends frequently.

Why does my WebSocket disconnect behind nginx? Almost always due to proxy inactivity timeout. Increase proxy_read_timeout and send a periodic ping shorter than the shortest timeout of all proxies along the path.

  • WebSocket
  • tiempo real
  • HTTP
  • backend
  • web

Keep reading

All articles · VPS plans