Skip to main content
Server-Sent Events (SSE) provide a simple way to stream real-time updates from the server to the client over HTTP. Unlike WebSockets, SSE is unidirectional (server to client only) and uses the standard HTTP protocol.

Basic SSE Endpoint

Create an SSE endpoint using EventSourceResponse and yield:
When you yield plain objects (dicts, Pydantic models, etc.), FastAPI automatically JSON-encodes them and sends them as SSE data: fields.

ServerSentEvent Model

For fine-grained control over SSE events, use the ServerSentEvent model:

ServerSentEvent Fields

data

The event payload. Can be any JSON-serializable value:
All data values, including plain strings, are JSON-serialized. For example, data="hello" produces data: "hello" on the wire (with quotes).

raw_data

Send raw string data without JSON encoding:
Use raw_data when you need to send pre-formatted text, HTML fragments, CSV lines, or any non-JSON payload. The data and raw_data fields are mutually exclusive.

event

Optional event type name that maps to addEventListener(event, ...) on the browser:
When omitted, the browser dispatches on the generic message event.

id

Optional event ID for resuming streams:
The browser sends this value back as the Last-Event-ID header on automatic reconnection.
The id field must not contain null (\0) characters.

retry

Optional reconnection time in milliseconds:
Tells the browser how long to wait before reconnecting after the connection is lost.

comment

Optional comment line(s) for keep-alive pings:
Comment lines start with : in the SSE wire format and are ignored by EventSource clients. Useful for preventing proxy/load-balancer timeouts.
FastAPI automatically sends keep-alive pings every 15 seconds to prevent connection timeouts. You don’t need to manually send comment events for this purpose.

Resuming Streams with Last-Event-ID

Handle reconnections by reading the Last-Event-ID header:

SSE with POST Requests

EventSourceResponse works with any HTTP method, including POST:
SSE over POST is useful for protocols like MCP (Model Context Protocol) that require streaming responses with request bodies.

Synchronous Generators

You can use regular (non-async) generator functions:

Client-Side JavaScript

Connect to SSE endpoints using the EventSource API:

SSE with POST (JavaScript)

For POST requests, use fetch with a ReadableStream:

SSE Wire Format

SSE events follow the text/event-stream format:
Each event is terminated by a blank line (\n\n).
FastAPI automatically handles SSE wire format encoding. You don’t need to manually format events.

When to Use SSE vs WebSockets

Use Server-Sent Events when:
  • You only need server-to-client communication
  • You want automatic reconnection with event replay
  • You prefer working over standard HTTP/HTTPS
  • You’re building notifications, live feeds, or progress updates
Use WebSockets when:
  • You need bidirectional communication
  • You require lower latency
  • You’re building chat applications or real-time collaboration tools
SSE has several advantages: automatic reconnection, event IDs for resuming streams, and compatibility with HTTP/2 multiplexing. It’s often the simpler choice for one-way streaming.