Skip to content
47% off: the complete course for $79 (reg. $149)Order now →
AI Builder

AI builder glossary

Server-sent events (SSE)

A simple HTTP protocol for pushing a one-way stream of text events from server to client, widely used to deliver streamed model output.

Server-sent events is a plain-text format over an ordinary HTTP response with the text/event-stream content type. Each event is a few field-and-value lines followed by a blank line, and the connection stays open while the server keeps writing. It flows one way, server to client, which is exactly the shape of a model response.

Model providers use SSE to stream tokens to your server, and many apps use it again to forward those tokens to the browser. Its appeal is simplicity: no special server, it works through most infrastructure, and it is easy to inspect when debugging.

The gotchas are practical. The browser's built-in EventSource only makes GET requests and can't send a JSON body, so chat features that POST a message usually read the stream from a fetch response and parse events themselves. Network chunks don't line up with event boundaries, so parsers must buffer until a blank line. Proxies and hosting platforms may buffer the response or close idle connections, so send periodic comment lines as heartbeats and disable buffering where you can.

For two-way, low-latency interaction such as realtime voice, WebSockets or WebRTC fit better.

Example

A Next.js route handler returns an event stream that forwards each text delta from the model, sends a heartbeat comment every few seconds while the model is thinking, and ends with a done event carrying token usage.

Learn more in the course

Related terms

Go from definitions to doing

Learn to build AI-powered apps in 132 practical lessons.