WebSockets

Let's learn about the WebSocket protocol and discuss its pros and cons.

Motivation

Most web APIs use HTTP as their underlying protocol to transfer data, and HTTP is often considered as one of the best options for executing batch tasksTasks that can be grouped together but performed independently and do not require an immediate response based on input factors. asynchronously. But when it comes to two-way and real-time communication such as chat, live streaming, gaming, and so on, HTTP falls short because it is a request-response protocol, where usually a server closes the connection after sending the response. We describe some HTTP-based techniques and their corresponding limitations in achieving bidirectional communication in the table below:

Technique

Description

Limitation

Short polling

Requests frequently for updates from the server after fixed short intervals. The server responds whether it has an update or not.

Sends too many unnecessary requests for updates

Long polling

Requests for updates from the server with the channel left open (based on some constraints), and the server responds when it has an update

As HTTP follows a request and response model. As a result, it uses multiple concurrent connections for sending data and receiving updates, leading to resource wastage.


HTTP streaming

HTTP streaming allows servers to stream bytes of data continuously over a single connection to the client while keeping the connection open

It suffers due to half-duplex communication

Note: The table above focuses on HTTP/1.1 because the other versions were not introduced when WebSocket was first developed.

We conclude from the above discussion that we need a different approach to achieve two-way data transfer between the server and client without waiting for clients' requests. We need an approach that has low latency and avoids TCP handshake by keeping the connection open indefinitely.

What is a WebSocket?

WebSocket was introduced in 2011 to enable full-duplexA simultaneous two-way communication of sending and receiving data. asynchronous communication over a single TCP connection to use resources efficiently. HTTP connection restricts TCP to a one-sided communication, where the client always starts the communication due to the request-response model. In other words, the client first sends requests, then the server responds to them, which is a half-duplex communication. In contrast, ...