WebSocket Protocol

The creation of the WebSocket protocol marks the beginning of the Living web. It is often referred
to as the first major upgrade in the history of web communications. As the Web itself ordinary did,
WebSocket enables entirely new kinds of applications. This protocol allowed for us that during
correspondence, exchanging messages with friends takes place in real time; to have a better user
experience when playing games; to follow live the change of data in the economic market, sports

events, but also in all other spheres, etc.

1. Definition

The official Request For Comments (RFC) describes the WebSocket protocol as follows:

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 mechanism for browser-based
applications that need two-way communication with servers that does not rely on opening multiple

HTTP connections.

2. The WebSocket handshake

The WebSocket protocol was to be released in an already existing web infrastructure. Therefore, it
has been designed to be backward-compatible. Before a WebSocket communication can start, an
HTTP connection must be initiated. The browser sends an Upgrade header to the server to inform
him he wants to start a WebSocket connection. Switching from the HTTP protocol to the
WebSocket protocol is referred to as a handshake.

GET ws://websocket.example.com/ HTTP/1.1
Origin: http://example.com
Connection: Upgrade
Host: websocket.example.com

Upgrade: websocket

If the server supports the WebSocket protocol, it sends the following header in response.

HTTP/1.1 101 WebSocket Protocol Handshake
Date: Wed, 5 May 2014 04:04:22 GMT
Connection: Upgrade

Upgrade: WebSocket

After the completion of the handshake the WebSocket connection is active and either the client or
the server can send data. The data is contained in frames, each frame is pre-fixed with a 4-12 bytes to ensure the message can be reconstructed.

Once the server and the browser have agreed on beginning a WebSocket communication. A first
request is made to begin an ethernet communication followed by a request to make an TCP/IP
communication.

3. Transport layer protocol

The internet is based on two transport layer protocols, the User Datagram Protocol (UDP) and the
Transmission Control Protocol (TCP). Both use the network layer service provided by the internet
protocol (IP).

TCP

TCP is a reliable transmission protocol. The data is buffered byte by byte in segments and
transmitted according to specific timers. This flow control ensure the consistency of the data. TCP

is said to be a stream oriented because the data is sent in independent segments.

UDP

UDP is unreliable but fast. The protocol offers no guaranty the data will be delivered in its
integrality nor duplicated. It works on the best effort strategy with no flow control. Each segments

are received independently, it is a message oriented protocol.

Websocket is build over TCP because of its reliability. Browser enabled games are the perfect
example of WebSockets use cases. They require low latency and have a high rate of update. To
achive low latency, the communication protocol must make sure not to drop any packets.

Otherwise, the exchange takes two times longer.

As can be inferred from the previous text, the websocket protocol relies on a few other protocols.
Namely, HTTP to initialize the communication, ethernet, TCP/IP and finally TLS in case a secure

connection is required.