WebSocket
protocol enable the two-way communication between client and server.
In 2011, the IETF standardized the WebSocket protocol as RFC 6455. Since
then,many browser started implementing and supporting web socket protocol. You can get the browsers that support web socket protocol
from below site.
Websocket
protocol extends the HTTP Protocol, by upgrading HTTP connection to a websocket. Once the connection is upgraded,then client and server
acts as two peers and can able to communicate in both directions and
able to transfer 'n' number of messages.
Basic
idea of web socket is very simple. Lets say you have a client and a
server. Then Websocket communication is established in 2 stages.
1.
Handshake
2.
Data Transfer
Handshake
Clients
and server communicate using HTTP. HTTP supports upgrade mechanism using 'Upgrade' header field. By using this field, you can upgrade
one version of HTTP to other version. WebSocket also uses this
mechanism to set up a connection with a HTTP server in a compatible
way.
First,
a client requests a websocket connection by using the "Upgrade:
websocket" and "Connection: Upgrade" headers, along
with a few protocol-specific headers to establish the version being
used and set-up a handshake.
The
server, if it supports the protocol, replies with the same "Upgrade:
websocket" and "Connection: Upgrade" headers and
completes the handshake. Once the Handshake completes then, both
peers can communicate 'n' number of messages.
Simple
terms, Websocket uses HTTP to upgrade the connection to support Websocket protocol. Once the Websocket protocol established, none of
the HTTP semantics valid on this connection.
How
the Handshake Request looks like
First
time client sends simple HTTP Request, and request server to upgrade
to websocket protocol.
GET /index HTTP/1.1 Host: localhost:8080 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Version: 13
'Upgrade:
websocket' tells to the server, that I want to upgrade to websocket.
How
the Handshake Response looks like
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: Nb+FaERLaX6uN3HUNNoFAAU8CPk=
If
server able to support websocket protocol, then it switches the
protocols from HTTP to websocket and send the response. The server
applies a known operation to the value of the Sec-WebSocket-Key
(Which send by client in request header) header to generate the value
of the Sec-WebSocket-Accept header. Client performs the same
operation on Sec-WebSocket-Key and computes a value. Checks the
computed value with the value of 'Sec-WebSocket-Accept' sent by
server. If the result matches the value received from the server.
The client and the server can send messages to each other after a
successful handshake.
Data
Transfer
Once
the Handshake done successfully, then client and server are
connected. Once the connection established, both client and server
acts as peers, i.e, they are capable of sending and receiving 'n'
number of messages. Client or server can initiate
the termination of connection at any point of time.
No comments:
Post a Comment