DEV Community

Guruharish B
Guruharish B

Posted on

Short polling ,Long polling And SSE

Polling:
Polling is the method in which client repeatedly checks server for new information

Short polling:
Short polling is the method in which client repeatedy checks server by sending request at regular interval of time to get new information

Example (Customer, Chef, and Servant)

  • Imagine a customer orders food in a restaurant, the chef is preparing food (server),client is waiting for the order he/she placed(client) and servant act as communication channel(api).

In Short Polling, the customer repeatedly asks the servant (whether the food is ready?)(whether the food is ready?) at regular interval of time(eg 5 seconds).
The servant goes to the chef every time, checks the status, and returns with an update, even if the food is not ready.

As the result, the customer keeps waiting and asking frequently.

Drawback:

  1. Too many request
  2. Delayed updates(If the food becomes ready right after a poll, the customer must wait until the next polling interval to receive the update)
  3. Network overhead

Long polling:

Long polling is the method in which the client sends a request to the server, and the server keeps the connection open until new data is available or a timeout occurs. The client also sets a timeout period for waiting for the response.

Example (Customer, Chef, and Servant)

  • Imagine a customer orders food in a restaurant, the chef is preparing food (server),client is waiting for the order he/she placed(client) and servant act as communication channel(api).

In Long Polling, the customer asks the servant **(Tell me when my food is ready)
**The servant goes to the chef and waits there.

  • If the chef finishes the food before the waiting time expires, the servant immediately returns and informs the customer.

If the chef does not finish the food within a certain time ,the servant says that the food is not read
The customer then asks again:
(Please let me know when it is ready)
The servant goes back to the chef and waits again.

Drawback

  1. Connection timeout issues
  2. Open connection for a long time.
  3. The servant waits continuously, making it inefficient for many customers.

Server Sent Events (SSE)
In Server-Sent Events (SSE), the client establishes a one way connection with the server and waits for updates. The server sends data to the client whenever new information becomes available, without requiring the client to repeatedly request updates.

Example
In ,live stock prices client make an SSE connection then server keeps the connection open.Whenever a stock price changes, the server notify the info to the client.
The client only receives data; it doesn't send messages through the SSE connection.(one way communication ie., server--> client)

Top comments (0)