Skip to the content.

⭐ What is SSE?

SSE (Server-Sent Events) is a way for the server to send real-time updates to the client over a single HTTP connection.

✔ One-way communication

✔ Uses normal HTTP

✔ Auto-reconnect

✔ Text/event-stream format


⭐ How SSE works (super simple explanation)

  1. Client connects to server using a URL:
const eventSource = new EventSource("/events");
  1. Server keeps the connection open.

  2. Whenever there is new data, the server pushes it to the client:

data: New message!
  1. Browser receives it instantly:
eventSource.onmessage = (e) => {
  console.log("Received:", e.data);
};

Easy explanation:

🟦 “Client opens a pipe. Server keeps sending updates through it.”


⭐ SSE vs WebSockets (super easy)

Feature SSE WebSockets
Direction One-way (server → client) Two-way (both ways)
Protocol HTTP WebSocket protocol
Complexity Very easy Medium
Real-time Yes Yes (even faster)
Auto-reconnect Yes No (must handle manually)
Best for Notifications, live scores Chat, games, collaboration apps

⭐ When to use SSE?

✔ Good for:

✔ Not good for:

Anything needing two-way communication


⭐ Why big companies use SSE?


⭐ Simple Server Example (Node.js)

app.get("/events", (req, res) => {
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");

  setInterval(() => {
    res.write(`data: ${new Date().toISOString()}\n\n`);
  }, 1000);
});

⭐ Simple Client Example

const es = new EventSource("/events");

es.onmessage = (e) => {
  console.log("Update:", e.data);
};

🎯 Final easy definition

SSE is a simple way for the server to push real-time updates to the client using a single long-lived HTTP connection, but only in one direction.