← Lessons

quiz vs the machine

Gold1340

Networking

Server Sent Events In Depth

How a one way text stream pushes updates over plain HTTP.

4 min read · core · beat Gold to climb

A Simpler Push

Not every app needs two way messaging. Server sent events give a server a one way channel to push updates to a browser over an ordinary long lived HTTP response.

The Stream Format

The server keeps the response open and writes a simple text stream. Each event is a block of lines.

  • A data line carries the payload, and several data lines join into one message.
  • An optional event line names a custom event type.
  • An optional id line lets the client track progress.
  • A blank line ends one event.

The browser parses these lines and fires events to the page as they arrive.

Automatic Reconnect

If the connection drops, the browser reconnects on its own. It sends the last seen id back in a header so the server can resume from where it left off.

  • This built in recovery is a major convenience.
  • A retry field can tell the client how long to wait before reconnecting.

Where It Fits

Server sent events suit notifications, live scores, and progress feeds. They are text only and one direction, so a chat that needs client messages still wants WebSockets.

Key idea

Server sent events stream simple text blocks over one long lived HTTP response for one way push, with automatic reconnect and last id resume making them ideal for live feeds.

Check yourself

Answer to earn rating on the learn ladder.

1. What transport do server sent events use?

2. How does a client resume after a dropped connection?