WebSocket is a stateful communication protocol which starts off as an HTTP/HTTPS exchange (with an upgrade header) that works at the application layer (Layer 7) of the OSI model, enabling bi-directional message exchange between client and web server. A WebSocket connection remains open until closed purposefully once messages are fully exchanged.
The WebSocket protocol is similar to the gRPC framework over HTTP/2 connections, which often outperforms WebSockets yet isn’t supported by most web browsers.
The protocol is also full-duplex, which means that client and web server can send each other messages simultaneously using a shared channel. It works similarly to a shared phone call or an instant message exchange between two people. Despite the channel being shared, communication happens smoothly without major performance or reliability issues.
This separates WebSocket from HTTP, for example, which is conversely half-duplex. This means that only one party (client or server) can communicate with the other at a time. While some associate WebSocket with HTTP, both internet protocols are functionally different and stand on their own. Another differentiator is HTTP's stateless nature. While a typical WebSocket request retains data between sessions, HTTP requires all portions of the message (request header, etc.) to be present in each request. Applications also use HTTP requests to serve static resources, whereas WebSocket better supports dynamic content.
What makes WebSocket useful?
A committee led by Michael Carter first introduced the WebSocket protocol in 2008. Since then, it's seen multiple revisions resulting in the following advantages:
Full-duplex functionality for bi-directional message exchange (previously mentioned)
Leverages a continuous connection, allowing data exchange from server to client without an explicit client request after the client establishes the WebSocket connection (enabling push functionality)
A small request footprint in terms of bytes
A relatively simple design with little overhead
Event-driven in nature
Widespread native browser support for WebSocket and the WebSocket API
WebSocket was designed to support applications that transmit updated data in real time. Some common use cases include multiplayer gaming, message-based chatting, real-time feeds, location apps, support for audio or video streaming chatting (via integration with WebRTC) and much more.
How does WebSocket work?
All WebSocket applications work by triggering an HTTP handshake to open a connection. This relies on an initial HTTP request, which uses a WebSocket Upgrade header to switch from HTTP to WebSocket. Here's what happens next:
The web server responds with a corresponding Upgrade header of its own, signaling that WebSocket will be the transmission protocol for the duration of the session.
The client can send WebSocket requests to the web server using a single TCP socket. Messages are sent in frames with a header and data payload, which functionally differs from alternatives such as UDP's datagrams.
These frames are sent with information about the message type, message length, and frame sequencing (is this frame the first or the last, for example).
The client and web server can communicate bi-directionally until either entity goes offline, or once the connection is purposefully closed.
There's also the WebSocket API, which helps clients and web servers communicate without polling the server and awaiting a reply. It supports the default WebSocket communicative mechanism with widespread support, or can enable the WebSocketStream feature. This uses the Streams API to circumvent common concerns over bottlenecking—regulating read and write performance based on system constraints and stream backpressure.
The WebTransport API has also emerged as a viable alternative to the WebSocket API, but needs to first iron out some wrinkles around cross-browser support and added complexity. However, the WebTransport API promises to reshape how WebSockets are used while addressing key shortcomings around reliability and message delivery sequencing.
Does HAProxy support WebSocket?
Yes! HAProxy products support HTTP (needed to initiate an initial WebSocket handshake) and also comprehensively support the WebSocket protocol without extra tooling. Managing a WebSocket application in HAProxy is often as simple as implementing timeout rules and connection closing.
To learn more about leveraging WebSocket in HAProxy, check out our WebSocket configuration tutorial.