𝗦𝗵𝗼𝗿𝘁 𝗣𝗼𝗹𝗹𝗶𝗻𝗴, 𝗟𝗼𝗻𝗴 𝗣𝗼𝗹𝗹𝗶𝗻𝗴, 𝗔𝗻𝗱 𝗦𝗦𝗘

You need to send data from a server to a client. Here are three ways to do it.

Short Polling

Think of a customer in a restaurant. The customer asks the waiter every 5 seconds if the food is ready.

The customer repeats this question at regular intervals.

The problems:

  • Too many requests hit your server.
  • Updates are delayed. If food is ready 1 second after a question, the customer waits 4 more seconds to ask again.
  • It wastes network resources.

Long Polling

The customer asks the waiter: Tell me when my food is ready.

The waiter stays at the table and waits. If the chef finishes the food, the waiter tells the customer immediately. If the food takes too long, the waiter tells the customer it is not ready yet.

The problems:

  • Connections stay open for a long time.
  • You might hit connection timeouts.
  • It is hard to manage many customers at once.

Server Sent Events (SSE)

Think of live stock prices. The client opens a connection once. The server keeps this connection open. Whenever a price changes, the server sends the update to the client.

Key facts:

  • This is one way communication.
  • Data flows from the server to the client only.
  • The client does not send messages back through this connection.

Source: https://dev.to/guruharish_b/short-polling-long-polling-and-sse-20o5