HTML and CSS Reference
In-Depth Information
Web Sockets
As discussed above, Server Sent Events continue to utilize standard HTTP requests and re-
sponses, but allow the server (rather than the client) to initiate requests.
Web sockets on the other hand are an API for real-time, bi-directional communication
between the client and server using a TCP based protocol. HTTP does use TCP as the un-
derlying transport protocol, but HTTP is designed for larger payloads, and is not intended to
be conversational.
Web Sockets are designed to allow the client and server to be as chatty with each other as
they like by imposing minimal overhead on each message sent and received. In addition,
Web Socket connections are full duplex: so it is possible to send and receive data simultan-
eously on the same connection.
Although Web Sockets uses a distinct protocol from HTTP, they do continue to use the same
HTTP ports (typically 80 and 443). This ensures that additional ports do not need to be
opened to use Web Sockets.
The initial handshake between the client and the server to establish the connection looks like
a regular HTTP GET request, except it contains a request to upgrade the HTTP connection:
GET /tasklist HTTP/1.1
Host: testing.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: f4JUFGdKI3ReHYt8JHETuo==
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Version: 13
The server will then respond with an HTTP request, confirming that the HTTP connection
has been upgraded to a Web Socket connection:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HjGT6d6YJHyyERmm5HY5TreKjWk=
Sec-WebSocket-Protocol: chat
Once the connection has been established, either the client or the server are able to initiate
the transfer of text data to the other party, and this communication will utilize the Web Sock-
et (TCP based) protocol.
Search WWH ::




Custom Search