What Is a WebSocket? WebSocket vs. HTTP, SSE and Webhooks
A WebSocket opens a two-way, always-on connection between browser and server. Here's the handshake, how it compares to polling, SSE and webhooks, and where it's used.

A WebSocket is a protocol that keeps a single TCP connection open between a browser and a server so both sides can send messages to each other at any time, in either direction, without the client having to ask first. It was standardized by the IETF as RFC 6455 in December 2011, and the WebSocket API that exposes it to JavaScript has been part of every major browser since. The practical effect: instead of a browser repeatedly polling a server with "anything new?" requests, a server can push a chat message, a price update or a game move to the browser the instant it happens, over a connection that stays open for as long as the page needs it.
- The WebSocket protocol is defined by IETF RFC 6455, published in December 2011.
- A WebSocket connection starts as a normal HTTP request that asks to "upgrade" to the WebSocket protocol; a compliant server replies with
101 Switching Protocols, and the same TCP connection then carries WebSocket traffic instead of HTTP. - Connections use the
ws://scheme, orwss://for the encrypted, TLS-secured version — the WebSocket equivalent ofhttp://vs.https://. - MDN marks the WebSocket API as "Baseline: widely available," meaning it has worked across major browsers since around July 2015.
- Unlike a webhook or a plain HTTP request, a WebSocket is full-duplex: both the browser and the server can send messages on the same connection at any time, independently of each other.
What is a WebSocket?
MDN's own description is direct: the WebSocket API "makes it possible to open a two-way interactive communication session between the user's browser and a server." Once that session is open, either side can send a message whenever it wants, and the other side receives it without having to make a request first. That's a meaningful departure from how the web normally works, where a browser is always the one initiating — asking for a page, a script, an image, a search result — and a server only ever responds to something it was asked, the request-response pattern covered in our plain-English guide to how an API works.
A WebSocket connection is identified by a URL using the ws:// scheme, or wss:// when it's secured with TLS, the same way https:// secures a normal web request. In JavaScript, opening one is a single line: new WebSocket("wss://example.com/chat"). From there, the browser fires events — open, message, close, error — as the connection's state changes, and the page can call .send() at any point to push data back to the server.
WebSocket vs. HTTP polling
Before WebSockets were standardized, developers who needed near-real-time updates in a browser had one real option: polling, or its closer cousin long-polling. Polling means the browser repeatedly asks a server "is there anything new?" on a fixed schedule — every few seconds, say — and the server replies "no" the vast majority of the time. It works, but every one of those "no" replies is still a full HTTP request and response, with its own headers and connection overhead, which adds up fast across thousands of connected clients. It also means genuine updates sit unseen for however long the gap between polls happens to be.
A WebSocket replaces that whole back-and-forth with one connection that both sides keep open. Instead of the browser asking repeatedly, the server simply sends a message the moment it has something to say. There's no polling interval to tune and no wasted "nothing changed" responses, because the connection isn't a sequence of separate requests at all — after the initial handshake, it's one ongoing conversation. This is a very different communication model from how a webhook works, which is still a single, one-way HTTP request fired by a server toward a URL you registered in advance; a webhook tells your backend something happened once, while a WebSocket keeps a live, two-way channel open to a browser for as long as the page is active.
The WebSocket handshake: ws, wss, and the 101 response
A WebSocket connection doesn't start life as something separate from HTTP — it starts as an ordinary HTTP request. The browser sends a GET request to the server carrying an Upgrade: websocket header, a Connection: Upgrade header, a Sec-WebSocket-Version header (the browser automatically sets this to 13, the version RFC 6455 defines), and a randomly generated Sec-WebSocket-Key value.
If the server supports WebSockets and agrees to the upgrade, it doesn't reply with a normal 200 OK. Instead, per RFC 6455, it responds with HTTP status 101 Switching Protocols, along with a Sec-WebSocket-Accept header containing a value derived mathematically from the client's key — proof that the server actually understood and accepted the specific request, not just any HTTP request. After that single exchange, the underlying TCP connection stops speaking HTTP entirely and starts carrying WebSocket frames in both directions. Both the browser's handshake request and the server's 101 response happen automatically; application code never constructs these headers by hand — the browser's WebSocket object handles all of it the moment you call new WebSocket(url).
WebSocket vs. webhook vs. Server-Sent Events
WebSockets, webhooks and Server-Sent Events (SSE) all exist to get data moving without the client constantly asking for it, but they solve different problems and aren't interchangeable:
| Technology | Direction | Connection | Typical use |
|---|---|---|---|
| WebSocket | Full-duplex (both sides, anytime) | One persistent TCP connection after the handshake | Chat, multiplayer games, live collaborative editing |
| Server-Sent Events | Server → client only | One persistent HTTP connection | Live feeds, notifications, streaming one-way updates |
| Webhook | Server → your server, one message | A new HTTP request per event, no standing connection | Payment confirmations, CI/CD triggers, backend-to-backend events |
The deciding factor is usually whether the browser itself needs to send data back over the same channel in real time. A stock ticker or a news feed only needs the server pushing data one way, which is what SSE is built for with far less protocol overhead than a full WebSocket. A chat app or a multiplayer game needs both sides talking continuously, which is exactly the case a WebSocket is designed around. And a webhook isn't really a browser technology at all — it's one backend notifying another backend's API endpoint that something happened, with no ongoing connection in either case.
How WebSocket messages flow once connected
After the 101 handshake completes, the WebSocket object in the browser exposes a small, consistent API. A readyState property reports one of four values — 0 (CONNECTING), 1 (OPEN), 2 (CLOSING) or 3 (CLOSED) — so application code can check the connection is actually open before trying to send on it. The send() method pushes a message to the server at any time once the state is OPEN, and an onmessage handler fires whenever a message arrives from the other direction, delivering it as a MessageEvent. onopen, onclose and onerror round out the lifecycle, firing when the connection is established, torn down, or runs into a problem.
MDN flags one real limitation worth knowing: the WebSocket API has no built-in backpressure handling. If a server sends messages faster than a browser tab can process them, that tab can run low on memory or become unresponsive, since there's no automatic mechanism to tell the sender to slow down. For applications pushing a very high volume of data, MDN points to the newer WebTransport API as a more capable alternative — but for the vast majority of chat, notification and live-update use cases, plain WebSockets remain the standard tool.
Common uses: chat, live dashboards, multiplayer games and trading
- Chat and messaging. Every message needs to appear on every connected participant's screen within a fraction of a second, in both directions, which is the textbook case for a persistent, full-duplex connection.
- Live dashboards and collaborative tools. Multiple people editing the same document or watching the same metrics dashboard need updates pushed to them the instant something changes elsewhere.
- Multiplayer games. Player positions, moves and game state need to travel in both directions with minimal delay, which is why browser-based multiplayer games are built almost exclusively on WebSockets rather than polling.
- Financial trading interfaces. Live price feeds and order updates need to reach the browser the moment they change, often alongside outbound messages like placing or canceling an order on the same connection.
WebSocket security: wss, origin checks and authentication
The single most important security decision in any WebSocket deployment is using wss:// instead of plain ws:// in production, exactly as using https:// instead of http:// matters for ordinary web traffic — it encrypts the connection with TLS so the handshake and every message that follows can't be read or tampered with in transit. Beyond encryption, because a WebSocket server accepts connections from a URL, it needs to actually check who's connecting: validating the Origin header on the initial handshake request helps prevent a malicious page on another site from quietly opening a WebSocket connection to your server using a logged-in visitor's browser.
WebSockets also don't carry their own built-in authentication scheme, so applications typically authenticate a connection the same way they'd authenticate any other request — a session cookie sent automatically with the handshake, or a token passed as part of the connection URL or an early message — and then re-validate that identity on the server before trusting anything the client sends afterward. Because the connection stays open far longer than a typical HTTP request, it's also worth deciding upfront how the server will handle a session that expires or a user who gets logged out while a WebSocket connection to them is still live.
Infrastructure sitting in front of a WebSocket server matters too. Because the protocol depends on that initial HTTP Upgrade request succeeding and then holding one TCP connection open for a long time, any load balancer, reverse proxy or CDN between the browser and the server has to be explicitly configured to pass the upgrade through and tolerate long-lived idle connections, rather than treating the request like an ordinary short-lived HTTP call. Cloudflare's own developer documentation notes that proxied WebSocket connections work without extra configuration on its network, but will still be closed automatically after a period with no traffic in either direction — a detail worth testing against, since an application that goes quiet for too long can find its "open" connection has actually been dropped by something in the middle.
Scaling a WebSocket service past a single server introduces one more wrinkle that a typical stateless REST API doesn't have: because each client holds a long-lived connection to one specific server process, routing a user's next request to a different server — the normal behavior of a load balancer — can mean they lose that connection entirely unless the architecture accounts for it, commonly through sticky sessions at the load balancer or a shared message bus that lets any server instance relay messages to a client connected elsewhere.
Getting started with your first WebSocket
On the browser side, nothing beyond standard JavaScript is required: const socket = new WebSocket("wss://example.com/chat"), followed by assigning onopen, onmessage, onclose and onerror handlers, is enough to establish a working connection against any compliant server. The harder half of the job is usually the server: it needs a library or runtime feature that understands the RFC 6455 handshake and can hold many connections open simultaneously without blocking, which is why most back-end frameworks and platforms — from Node.js to Cloudflare's own edge Workers — ship dedicated WebSocket support rather than expecting developers to implement the handshake by hand. For most teams building a first real-time feature, reaching for that built-in support, and testing the full open-send-receive-close lifecycle against a real server early, is the fastest way to confirm the connection behaves the way the application actually needs it to.
Frequently asked questions
What is a WebSocket in simple terms?
A WebSocket is a protocol that keeps one connection open between a browser and a server so either side can send messages to the other at any time, instead of the browser having to repeatedly ask for updates.
What is the difference between a WebSocket and HTTP?
A normal HTTP request-response ends as soon as the server replies, and the browser must always initiate. A WebSocket starts as an HTTP request but then upgrades into one long-lived connection where both the browser and the server can send messages independently.
What is the difference between a WebSocket and a webhook?
A webhook is a single one-way HTTP request a server sends to a URL you registered when an event happens, with no ongoing connection. A WebSocket keeps a two-way connection open between a browser and a server for as long as the page needs it.
What is the difference between a WebSocket and Server-Sent Events (SSE)?
SSE sends data in one direction only, from server to browser, over a persistent HTTP connection. A WebSocket is full-duplex, letting both the browser and the server send messages independently on the same connection.
Are WebSocket connections secure?
They can be, using wss:// instead of ws://, which encrypts the connection with TLS the same way https:// secures ordinary web traffic. Applications also need to validate the Origin header and authenticate connections themselves, since WebSockets have no built-in authentication scheme.
Sources
- IETF: RFC 6455 — The WebSocket Protocoldatatracker.ietf.org
- MDN Web Docs: WebSocket API (WebSockets)developer.mozilla.org
- MDN Web Docs: WebSocket interface referencedeveloper.mozilla.org
- Cloudflare Docs: WebSocketsdevelopers.cloudflare.com
Felix Moreau writes Pandromeda's software coverage and how-to guides. He covers Windows, macOS and Linux updates, the apps people rely on, emulators and developer tools, and turns official documentation into clear, numbered steps that work on the current version.

