Vergleichsskizze: links fragt ein Browser bei klassischem Polling immer wieder einzeln beim Server nach und erhält meist die Antwort "nichts Neues"; rechts besteht nach einem einmaligen Handshake eine dauerhaft offene WebSocket-Verbindung, über die Server und Browser in beide Richtungen jederzeit Nachrichten senden.

WebSocket

WebSocket is a technique that lets browsers and servers stay permanently connected and send data in both directions at any time. It's the foundation for chats, live stock market data feeds, and other applications where updates need to arrive instantly.

When you visit a normal website, your browser asks a computer on the network, and that computer sends the page back. After that, the contact ends. If you want to know something new, you have to ask again. WebSocket turns this principle around: browser and remote end stay permanently connected to each other. Over this open line, both sides can send something at any time on their own initiative, without anyone having to ask first. The difference is roughly that between exchanging letters and making a phone call.

Why waiting is measured in milliseconds

Before WebSocket, people helped themselves with a trick called polling. The page simply asked once a second whether there was anything new. Most of the time, the answer was “no”. Yet every one of these requests still costs computing time and data traffic, and it does so for every single user. With a hundred thousand simultaneous visitors, this results in a hundred thousand useless requests per second.

On top of that comes the delay. Anyone who only checks once a second learns about news half a second too late on average. For a weather report, that doesn’t matter. When trading stocks or cryptocurrencies, it does. There, milliseconds decide the price at which an order gets executed.

WebSocket solves both problems at once. The connection is already established, so there’s no need to keep rebuilding it. And the server can pass on a message the very moment it arises. That’s exactly why chats, online games, price tickers, and the gradually appearing responses of AI chatbots run on this technology today.

From handshake to open line

A WebSocket connection begins quite normally. The browser sends an ordinary request to the server, but attaches a request to switch to WebSocket. Experts call this process a handshake. If the server agrees, it responds with a confirmation, and from that moment on the same line is used differently. So no second connection is opened; instead, the existing one is repurposed.

From this point on, all the administrative text that every normal web request carries along disappears. Instead of several hundred characters of header data per message, a few bytes suffice. The data is packed into small packets called frames. Each frame contains hardly more than an indication of its length and whether it carries text or raw data.

So that the line doesn’t drop unnoticed, both sides regularly send each other brief signs of life, so-called pings. If no response comes back, the connection is considered dead and gets rebuilt. Encrypted connections can be recognized by the address: it starts with wss instead of ws, similar to https instead of http.

Where an open line is running in the background

You probably use WebSocket every day without noticing. Every chat app in the browser works with it, as do shared documents where you instantly see other people’s changes. The notification that someone is currently typing also comes via this route. In browser-based online games, the technology is standard anyway.

In the financial sector, WebSocket is especially visible. Stock exchanges and crypto trading platforms almost always provide their price data via WebSocket interfaces. Anyone writing a trading program subscribes to a data stream there and receives every price change as it happens. Providers often differentiate their pricing plans by how many such streams you’re allowed to keep open at the same time.

A common misconception is that WebSocket is always the better choice. That’s not true. For data that rarely changes, a normal request is simpler and more robust. And open connections permanently occupy memory on the server, which makes operation expensive with very many users. The technology makes sense where news arrives unpredictably and frequently.

Related Products

Latest News

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.