Introduction to WebSockets :: Part - 2


Bhaskar S 02/14/2014


Introduction

In Part-1 we got our hands dirty with a practical example in WebSockets.

But, how do WebSockets really work behind the scene ???

In this part, we look under-the-hood to understand the mechanics behind WebSockets.

WebSockets - Under the Hood

Launch Firefox and enter the following URL:

We should see the browser like the one shown in Figure-1 below:

SimpleMonitor Open
Figure-1

When we click on the Start Monitor button, a HTTP GET command is issued as shown in Figure-2 below:

Wireshark Client Request
Figure-2

This is the opening handshake request message from the client to the server.

The Request URI /polarsparc/SimpleMonitor after GET is used to identify the WebSocket server endpoint.

The Host request header allows both the client and the server to verify and agree which host to use.

The Sec-WebSocket-Version request header allows the client to indicate the WebSocket protocol version it plans to communicate with. If incompatible with the version supported by the server, then the handshake is terminated by the server.

The Origin request header indicates the origin of the client that is initiating the WebSocket connection. The server can use this information to determine whether to accept the incoming client connection. If the server does not wish to accept this client connection, it will terminate the handshake.

The Sec-WebSocket-Key request header is a BASE64 encoded value that is used by the server to send a corresponding response header to indicate acceptance of the client WebSocket connection.

The Connection request header from the client must include the Upgrade string.

The Upgrade request header from the client must contain the protocol string websocket. Upon receiving this header from a client, the server will attempt to switch to the requested protocol, which is WebSocket in this case.

In order to successfully complete the client WebSocket connection request, the server will respond with a message as shown in Figure-3 below:

Wireshark Server Response
Figure-3

This is the opening handshake response message from the server to the client.

The server must send a HTTP status code of HTTP/1.1 101 Switching Protocols to indicate the server is switching protocols from HTTP to WebSocket.

The Upgrade response header from the server must contain the protocol string websocket. Upon receiving this header from the server, the client will attempt to switch to the requested protocol, which is WebSocket in this case.

The Connection response header from the server must include the Upgrade string.

The Sec-WebSocket-Accept resonse header is a BASE64 encoded value. It is computed by taking the value from the client request header Sec-WebSocket-Key, appending it with a Globally Unique Identifier (GUID) string of "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" and creating a BASE64 of the SHA-1 of the concatenated value. The presence of this respobnse header indicates that the server has accepted the client websocket connection. The client will validate this value upon receiving the response from the server.

This completes the initial handshake between the client and the server and at this point a WebSocket connection is established between the client and the server.

The client and the server can now comunicate with each other in full-duplex using WebSocket messages. At the protocol level the WebSocket messages are known as WebSocket Data Frames.

In our example, the client sends a text WebSocket message once after which the server starts sending CPU metrics as text WebSocket messages to the client at regular intervals (every 5 seconds in our example). The following is an example of a WebSocket message from the server to the client captured at the network protocol level:

Wireshark WebSocket Message
Figure-4

A WebSocket Data Frame at the protocol level is defined as shown in Figure-5 below:

WebSocket Data Frame
Figure-5

The following is the explanation of the various fields from the WebSocket Data Frame:

When we click on the Stop Monitor button, a close WebSocket message is sent by the client to the server as shown in Figure-6 below:

Wireshark WebSocket Client Close
Figure-6

When the server endpoint receives a close WebSocket message from a client, the server will respond with a close WebSocket message to the client as shown in Figure-7 below:

Wireshark WebSocket Server Close
Figure-7

A this point the WebSocket connection between the client and the server is closed.

References

Introduction to WebSockets :: Part - 1