Server-Sent Events (SSE) stream text from server to browser in one long text/event-stream response. Each message is field: value lines ended by a blank line, such as id: 3, event: price, data: {"n":3}. EventSource parses them and reconnects automatically, sending Last-Event-ID so the server can resume. SSE is Baseline Widely available (since July 2022). This Node server ends its response after every three events:
const source = new EventSource('/events');
source.addEventListener('price', (e) => {
console.log(`id ${e.lastEventId}: ${e.data}`);
if (e.lastEventId === '4') source.close();
});
source.onerror = () => console.log('stream ended, reconnecting...');Output
id 1: {"n":1}
id 2: {"n":2}
id 3: {"n":3}
stream ended, reconnecting...
id 4: {"n":4}EventSource sends only plain GET requests, so clients of the OpenAI 86 and Anthropic APIs, which stream answers to a POST as SSE, parse fetch() bodies instead. Over HTTP/1.1 each stream holds one of six connections per host.