WebSocket keeps one connection open so servers can speak first

WebSocket keeps one connection open so servers can speak first
Image: Intellect (generated)

In short: Normally a web page has to ask before a server answers. WebSocket changes that by holding a single connection open, letting a server push messages to a browser without being asked. The idea is simple, yet it solved a real problem for live chats, games and dashboards that must update in real time.

WebSocket is a communications protocol that provides a two-way channel over one TCP connection. The IETF standardized it as RFC 6455 in 2011, and the browser-facing version, known as WebSockets, is now a living standard maintained by the WHATWG. The protocol began as a placeholder in the HTML5 specification, and in June 2008 a series of discussions led by Michael Carter produced the first version. Google Chrome 4 was the first browser to deliver the feature with full support, in December 2009, enabled by default from the start.

The key difference from ordinary web traffic is the direction of conversation. Standard HTTP is half-duplex: the client asks and the server replies. WebSocket is full-duplex, so either side can send data whenever it likes. Earlier workarounds such as Comet channels or Adobe Flash Player could approximate this, but Comet was tricky to build and, because of TCP handshake and HTTP header overhead, inefficient for small messages. WebSocket was designed to avoid that cost while keeping the security assumptions of the web intact.

A connection starts with an HTTP request that asks to switch protocols. The server answers with status code 101, Switching Protocols, and from then on the exchange uses a binary frame-based format. Because the handshake looks like HTTP, it can share the standard web ports, 443 for encrypted traffic and 80 for unencrypted, which helps it pass firewalls that block other kinds of traffic. Security still needs care: WebSocket requests are exempt from the same-origin policy, so servers must check the Origin header during setup to block cross-site hijacking, and sensitive connections are better guarded with tokens than with cookies alone. RFC 7692 later added a DEFLATE compression extension applied per message.

Opens the Intellect+ feed with this card first. Swipe up for the next one.

Sources

To cite this card, link to https://intellectplus.co.uk/app/technology-websocket.

How we source, check and correct cards: Editorial standards.

General information, not professional advice. Spotted an error? Flag the card or see corrections and complaints.

Intellect Plus is a visual educational discovery platform for exploring fascinating facts and ideas. Brain Food, Not Brain Rot. What is Intellect Plus?

Related

Finding something worth knowing…