WebSockets communicates using the TCP layer. The connection is established over HTTP and is basically a handshake mechanism between the client and server. After the handshake, the connection is upgraded to TCP. Let's see how it works through this flow diagram:
The following steps will take you through the flow shown in the preceding diagram:
The first step is the HTTP call that is initiated from the client side; the header of the HTTP call looks like this:
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com
Here,
Host
is the name of the server that we are hitting.Upgrade
shows that it is an upgrade call for, in this case, WebSockets. Connection defines that it is an upgrade call.Sec-Websocket-Key
is a randomly generated key which is further used to authenticate the response. It is the authentication key of the...