Skip to content

Real-Time Updates with Server-Sent Events (SSE)

Beginning of the lesson

Real-Time Updates with Server-Sent Events (SSE)

A lightweight one-way channel for live data over plain HTTP

Big idea: Server-Sent Events (SSE) let a server push a continuous stream of updates to a client over a single, long-lived HTTP response. There is no new protocol and no upgrade handshake: it is ordinary HTTP whose body is a text/event-stream that simply never finishes. For dashboards, notifications, price tickers and progress bars -- anything where data flows server -> client and rarely the other way -- SSE is often simpler and more robust than a full WebSocket. You have already met WebSockets in this course; this lesson gives you the lighter tool for the many one-way cases.
By the end you will be able to:
  • explain how SSE streams events over one keep-alive HTTP response;
  • write the text/event-stream wire format -- id, event, data, retry and comments;
  • consume a stream in the browser with EventSource and named events;
  • use automatic reconnection and Last-Event-ID to resume without gaps;
  • keep connections alive with heartbeats and defeat proxy buffering;
  • decide honestly when SSE is the right tool and when WebSockets are.

1. What SSE is, and why it exists

The classic way to get fresh data from a server is polling: the client asks "anything new?" every few seconds. Polling wastes requests when nothing changed and still adds latency when something does. SSE inverts this. The client opens the connection once, and the server keeps that response open and writes new events into it whenever they occur. The browser exposes this through a tiny built-in API called EventSource.

Because the transport is just HTTP, SSE inherits everything HTTP already gives you: it works over the same port as your web app, passes through firewalls and proxies that allow normal web traffic, and is trivial to secure with TLS and cookies. There is no separate protocol to open in your infrastructure.

Analogy. Polling is like phoning the train station every minute to ask if your train is late. SSE is a radio broadcast: you tune in once, and announcements arrive as they happen. You do not talk back on the radio -- and for departures you do not need to. That one-way shape is exactly what SSE is built for.

SSE is a browser standard (part of HTML), so the client side is not something you invent -- the reconnection, the event parsing, and the Last-Event-ID handling are all done for you by EventSource. Your job on the server is only to speak the simple wire format correctly.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers