Low-latency applications typically require a faster means of communication than what HTTP can deliver. Examples include multiplayer online gaming or chat applications where real-time, interactive communications are needed to provide the right user experience. WebSockets is a real-time communication protocol that enables an open, continuous, two-way connection between a client and a server. This enables data to be sent to a client (i.e., a browser), even without the client having to request it. With the HTTP protocol, the server only responds if the client sends a request.
Financial institutions are another use case for WebSockets. Recently, we worked with a major North American bank using the WebSocket protocol to establish an open connection for their origin server to send real-time notifications whenever trades go through without the client polling for them.
How does the WebSocket protocol work?
WebSocket connections start as regular HTTP connections. To establish a WebSocket connection, the client and server upgrade from the HTTP protocol to the WebSocket protocol during their initial handshake (this protocol switch is known as the WebSocket handshake). To initiate this upgrade, the client sends a request to the server using the following headers:
Connection: Upgrade – indicates that the client wants to change the protocol
Upgrade: WebSocket – indicates that the requested protocol is “WebSocket”
If the server understands the WebSocket protocol and agrees to the switch, it sends back a special response code: 101 (Switching protocols) with the same two headers. At that point, the HTTP connection is replaced by the WebSocket connection over the same underlying TCP/IP connection. Once established, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode until one side closes the connection.
The key benefits of WebSockets over HTTP include:
Persistent connection: No need for the client to frequently reinitiate connections checking for new data.
Lower latency: WebSockets use minimal overhead. Because there is a continuous connection, the normal overhead associated with HTTP requests, such as headers and other information, no longer needs to be sent, which reduces latency.
Full-duplex communication: WebSockets offer bidirectional communications between the server and the client so that both parties can communicate and exchange data at the same time. Edgecast, now Edgio, is pleased to offer support for the WebSocket protocol across our worldwide delivery network. As we move into the IoT and edge computing age, the WebSocket protocol will benefit companies wanting to offer low-latency applications at the edge.