𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗦𝗲𝗿𝘃𝗲𝗿 𝗦𝗲𝗻𝘁 𝗘𝘃𝗲𝗻𝘁𝘀

Imagine you use ChatGPT. You ask a question. Instead of seeing words appear one by one, you wait 5 seconds for the whole answer to appear at once.

This delay feels slow. It feels frustrating.

To fix this, developers use streaming responses. This makes the experience feel fast.

Server Sent Events (SSE) make this possible.

What is SSE? SSE is a standard for streaming data from a server to a client via HTTP. It allows the server to push updates to you in real time.

How it works: The server sends small text blocks. These blocks use fields like data, event, id, and retry. Each block is separated by a blank line.

Example: data: {"name": "Portal Gun", "price": 999.99}

Common uses for SSE:

  • AI chat streaming
  • Live notifications
  • Real time logs
  • System observability

Before SSE, developers used two main methods to get updates:

  1. Short polling The client asks the server for data. If the data is not ready, the server sends a wait message. The client waits and asks again. This repeats until the work is done.

  2. Long polling The client asks the server for data. The server holds the request open until data is ready or a timeout happens. The client must restart the request immediately after receiving a response.

Event streams handle continuous processes. These processes follow a specific order. This includes simple tasks like creating a PDF or complex tasks like bank transactions.

The server opens a communication channel with the client. It keeps this channel open until the process finishes.

Source: https://dev.to/rishini_dharan_t/understanding-server-sent-events-3mkd