BroadRouter
Get started

Server-sent events (SSE)

Updated

Server-sent events (SSE) is a web standard for a server to push a stream of text events to a client over one long-lived HTTP response with the content type text/event-stream. Each event is one or more field lines, such as data:, ended by a blank line.

Also called: SSE, server-sent events, text/event-stream, event stream, EventSource

SSE is the plain-text framing under almost every streamed LLM reply. Knowing the format helps when an SDK is not available or a stream misbehaves.

How it works

The WHATWG HTML standard defines the format. The response has MIME type text/event-stream and is always UTF-8. The body is lines of field: value:

: keep-alive comment, ignored by clients

event: update
id: 42
data: first line
data: second line

data: {"choices":[{"delta":{"content":"Hi"}}]}

data: [DONE]

The parsing rules that matter:

  • A blank line dispatches the event. Until it arrives, the client buffers.
  • Several data: lines join with newlines into one message.
  • A line starting with a colon is a comment. Servers send these to keep idle connections open.
  • One space after the colon is stripped. Lines may end in CRLF, LF or CR.
  • event, id and retry set the event type, the last event ID and the reconnection delay in milliseconds.

Network reads do not line up with events. One read can hold half an event or three of them, so a parser must buffer bytes and split on blank lines, not on reads.

Reconnection, proxies and [DONE]

The standard builds in recovery for browsers: after a drop, EventSource reconnects and sends a Last-Event-ID header, and a server can stop retries with HTTP 204. The spec also warns that “legacy proxy servers” may drop HTTP connections after a short timeout, and suggests a comment line “every 15 seconds or so”.

LLM APIs use less of this. OpenAI’s Chat Completions stream sends only data: lines and ends with data: [DONE], a convention of the API, not of SSE. There is no event ID to resume from, so a dropped stream cannot be continued; a retry is a new request. The general picture of streaming and the time-to-first-token benefit are covered on that page.

On BroadNet Inference

BroadNet’s docs say to set stream=true and “read Server-Sent Events until the literal data: [DONE] marker”. If the connection ends before it, treat the response as incomplete, record the X-Request-ID header, and decide whether a new inference request is safe. With stream_options: {"include_usage": true}, usage arrives in the last chunk. The docs’ curl example uses -N so curl does not buffer. A stream also holds one concurrency slot until it ends: 4 on a pay-as-you-go key, one on an hourly key. The docs ask you to verify streaming per model before production. Code for each SDK is in the streaming guide, and the chunk contents are explained in LLM streaming explained.

Questions

Can I use the browser EventSource API with an LLM API?

Usually not directly. The spec's EventSource constructor takes only a URL and a withCredentials flag, so it cannot send a POST body or an Authorization header. Use fetch and parse the stream yourself, or an SDK.

What does "data: [DONE]" mean?

It is not part of the SSE standard. OpenAI-style APIs send the literal line "data: [DONE]" as their last event to say the reply is complete.

Why does my stream arrive all at once?

Something between the server and your code is buffering the response, such as a reverse proxy, a framework or curl without -N. Turn buffering off for that route.

Read as MarkdownAsk ChatGPTAsk ClaudeAsk Perplexity