Polling vs WebSockets

Rajkumar
3 min readMay 26, 2021

How data flows between client and server. What is the difference between Long-Polling, WebSockets

Photo by Christopher Robin Ebbinghaus on Unsplash

Scenario

In a one-to-one chatting application, we have two clients and a server. When client_1 sends a message to client_2, it establishes a connection with the server and pushes the message by HTTP POST request.

How does client_2 know there is a message on the server?

Client_2 has to periodically connect with the server and ask for any new messages for it by HTTP GET request. If any new messages, then the server responds with the message or sends an empty response.

How frequently should a client ask the server for the new messages (send HTTP GET request)?

Every 1 min? lag in the conversation, every 1 sec? server will be overwhelmed with the requests, which degrades the server performance, most of the time, the server has to respond with empty responses.

Long Polling

In long polling, the client asks the server for the message. The server responds if it has the message, else it keeps the connection open until it has something to send instead of sending an empty response immediately. This way, the client no need to bombard the server.

When the client receives the message, it immediately re-requests the information from the server. This way, there is always an active connection between client and server.

The timeout logic is set on the client-side; the small example in javascript illustrates how the client establishes the connection with the server.

simple client-side long polling snippet

Pros

  • Long Polling is simple to implement. There are no many code changes required as illustrated in the above example.

Cons

  • Intensive processing applications can experience a lag in continuous transmission to the client.
  • Different gateways have different opinions about how long a connection should be open. Anything will kill it if it remains open for too long even in between some important transaction.

WebSocket

Websockets provide a full-duplex connection over a single TCP connection. WebSockets allows data to flow between client and server simultaneously. A “handshake” between the client and the server creates a TCP connection. Both the server and the client will send messages back and forth at any time. As a result, the connection remains active until one of the two parties expressly terminates it.

Pros

  • WebSockets maintains a single link, thus removing the latency issues that come with Long Polling.
  • Headers are not sent whenever more information is required from the server. As a result, the amount of data sent to the cloud becomes less costly.
  • Most firewalls do not need any reconfiguration by using WebSockets.

Cons

  • When links are terminated, WebSockets do not immediately recover. We must carry out our implementation.

The are various libraries available to work with the WebSockets and Node JS, few are socket.io, WS, sockjs etc.

A simple client snippet to connect with the server using WebSocket

React snippet — Client-side socket implementation

A simple Server snippet to connect with the client using WebSocket.

Express snippet — Server-side socket implementation

--

--