Long Polling Vs SSE Vs WebSocket

Priya Srivastava
3 min readDec 27, 2023

--

Whenever we design the client server model , client is usually the initiator of the request to the server . And the server send response to these requests .

However there comes the usecases where we want the server to response much more frequently or the server needs to provide more information than the client is asking for .

Thats where we use all these techniques.

Example :

Where it is used : Example : You heard of Zerodha App , in the architecture of zerodha app , when the app connects with payment gateway to have the succesfull payment , it uses long polling to receive whether the payment system has successfully done the payment .

Zerodha WatchList api which looks for stocks update might use SSE to get the events from server whenever there is update in stock prices .

Live Streaming platforms also use SSE .

Gaming Application , Whatsapp that you use uses websocket because there lies the requirement of full duplex connection between server and client for communication.

HTTP LONG POLLING :

Suppose we have client server type architecture where client sends the request . And client is waiting for server to send the response .

Immediately when clients receives the response . It again sends the new request to the server .

So we are polling from the client to server and waiting till the time it is receiving the response , as soon as it receives it , it send the new polling request .

With Long-Polling, the client requests information from the server exactly as in normal polling, but with the expectation that the server may not respond immediately. That’s why this technique is sometimes referred to as a “Hanging GET”.

Protocol : HTTP -> application/json -> text/palin

SSE :

Under SSEs the client establishes a persistent and long-term connection with the server. The server uses this connection to send data to a client. If the client wants to send data to the server, it would require the use of another technology/protocol to do so.

  1. Client requests data from a server using regular HTTP.
  2. The requested webpage opens a connection to the server.
  3. The server sends the data to the client whenever there’s new information available.

SSEs are best when we need real-time traffic from the server to the client or if the server is generating data in a loop and will be sending multiple events to the client.

Protocol : HTTP -> text/event stream

WEBSOCKETS :

WebSocket provides Full duplex communication channels over a single TCP connection.

It provides a persistent connection between a client and a server that both parties can use to start sending data at any time. The client establishes a WebSocket connection through a process known as the WebSocket handshake.

The WebSocket protocol enables communication between a client and a server with lower overheads, facilitating real-time data transfer from and to the server.

Protocol : ws:// or wss://

Long Polling Vs SSE Vs WebSockets

Credits : https://www.educative.io/courses/grokking-modern-system-design-interview-for-engineers-managers

Image Credit : Tech Primer

--

--

Priya Srivastava

Focussing on the improvising the knowledge and sharing the journey of starting from scratch and then become the master of all.