Previous Topic

Next Topic

Book Contents

Book Index

Technology Overview

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a communication mechanism for two-way communication with servers that does not rely on opening multiple HTTP connections.

The WebSocket Protocol is designed to supersede existing bidirectional communication technologies that use HTTP as a transport layer to benefit from existing infrastructure (proxies, filtering, authentication). Such technologies were implemented as trade-offs between efficiency and reliability because HTTP was not initially meant to be used for bidirectional communication. The WebSockets Protocol attempts to address the goals of existing bidirectional HTTP technologies in the context of the existing HTTP infrastructure; as such, it is designed to work over HTTP as well as to support HTTP proxies and intermediaries.

Protocol Handshake

The WebSockets Protocol provides a persistent bidirectional connection between a client and server that both parties can use to start sending data at any time. The client establishes a WebSocket connection through a process known as the WebSocket handshake. This process starts with the client sending a regular HTTP request to the server. The HTTP request contains the Upgrade: websocket and Connection: Upgrade headers that inform the server that the client wishes to establish a WebSocket connection.

The protocol has two parts: a handshake and the data transfer. The handshake from the client looks as follows:

GET /chat HTTP/1.1
        Host: server.example.com
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
        Origin: http://example.com
        Sec-WebSocket-Protocol: chat, superchat
        Sec-WebSocket-Version: 13

If the server supports the WebSocket protocol and has a support for at least one of the demanded by the client sub-protocols (chat and superchat), it agrees to the upgrade and communicates this through an Upgrade header in the response and via the Sec-WebSocket-Protocol HTTP header it shows which sub-protocol it supports.

The handshake from the server looks as follows:

HTTP/1.1 101 Switching Protocols
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
        Sec-WebSocket-Protocol: chat

The Sec-WebSocket-Protocol header is optional. If present and the server does not support any of the requested sub protocols the request is handled as normal HTTP request (no WS connection). If the Sec-WebSocket-Protocol is missing the WebSockets Server uses the default sub-protocol.

How an Established WebSocket Work

Once the handshake is complete the initial HTTP connection is replaced by a WebSocket connection that uses the same underlying TCP/IP connection. At this point either party can starting sending data. Data is transferred through a WebSocket as messages, each of which consists of one or more frames containing the payload. In order to ensure the message can be properly reconstructed when it reaches the client each frame is prefixed with 4-12 bytes of data about the payload, shown in the diagram below. This frame-based messaging system reduces the amount of non-payload data that is transferred, leading to significant reductions in latency and bandwidth. for further details see RFC 6455.

0                   1                   2                   3
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
     +-+-+-+-+-------+-+-------------+-------------------------------+
     |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
     |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
     |N|V|V|V|       |S|             |   (if payload len==126/127)   |
     | |1|2|3|       |K|             |                               |
     +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
     |     Extended payload length continued, if payload len == 127  |
     + - - - - - - - - - - - - - - - +-------------------------------+
     |                               |Masking-key, if MASK set to 1  |
     +-------------------------------+-------------------------------+
     | Masking-key (continued)       |          Payload Data         |
     +-------------------------------- - - - - - - - - - - - - - - - +
     :                     Payload Data continued ...                :
     + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
     |                     Payload Data continued ...                |
     +---------------------------------------------------------------+

References