WebSockets
Let's learn about the WebSocket protocol and discuss its pros and cons.
We'll cover the following...
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
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