sse
**Server-Sent Events (SSE)** is the **HTTP-based server-to-client streaming protocol that enables web servers to push real-time updates to browsers over a single persistent HTTP connection** — the standard technology behind LLM token streaming (the "typing" effect in ChatGPT, Claude, and other AI interfaces) because it works over standard HTTP, requires no special client libraries, and is automatically reconnecting.
**What Is SSE?**
- **Definition**: A W3C standard (EventSource API) for servers to push a stream of text events to browsers over a standard HTTP connection — the response stays open and the server sends events formatted as "data: content
" whenever it has updates to deliver.
- **One-Way Streaming**: Unlike WebSockets (bidirectional), SSE is strictly server-to-client — the client sends one HTTP request, then listens. For LLM token streaming, this is sufficient since the client sent the prompt in the initial POST and the server streams the response.
- **Text/Event-Stream**: The Content-Type for SSE is text/event-stream — the server keeps the response open and sends events in a specific text format: event name (optional), data, and retry interval.
- **Auto-Reconnect**: Browser EventSource API automatically reconnects if the connection drops — servers can include the last event ID so clients resume from where they left off after reconnection.
- **Works Over HTTP/1.1**: SSE requires no protocol upgrade (unlike WebSockets) — works through HTTP proxies, load balancers, and CDNs without special configuration, simplifying deployment.
**Why SSE Matters for AI/ML**
- **LLM Token Streaming**: Every major LLM API (OpenAI, Anthropic, Gemini) uses SSE for streaming responses — the client POSTs a request with stream=true and receives a stream of "data: {...}" events, one per token or token group, creating the real-time typing effect users expect.
- **Simple Implementation**: Streaming LLM responses requires only a few lines of server code — no WebSocket library, no connection state management, no heartbeat logic. FastAPI SSE streaming is trivially simple.
- **Training Progress Streaming**: ML training job dashboards stream loss/accuracy updates via SSE — the browser automatically reconnects if the server restarts (e.g., after a checkpoint), resuming the stream without user intervention.
- **AI Pipeline Progress**: Long-running AI tasks (document processing, batch embedding, evaluation runs) stream progress events via SSE — users see real-time updates without polling endpoints.
**SSE Event Format**:
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
data: {"token": "The", "index": 0}
data: {"token": " answer", "index": 1}
data: {"token": " is", "index": 2}
event: done
data: {"finish_reason": "stop", "total_tokens": 42}
**FastAPI SSE Streaming (LLM)**:
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import json
app = FastAPI()
@app.post("/generate")
async def generate(request: dict):
async def event_stream():
async for token in llm.stream(request["prompt"]):
yield f"data: {json.dumps({"token": token})}
"
yield "data: [DONE]
"
return StreamingResponse(event_stream(), media_type="text/event-stream")
**OpenAI Streaming (SSE client)**:
from openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Explain SSE"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
**Browser EventSource API**:
const source = new EventSource("/training-progress");
source.onmessage = (event) => {
const data = JSON.parse(event.data);
updateChart(data.step, data.loss);
};
source.onerror = () => {
// Auto-reconnects automatically
};
**SSE vs WebSockets**
| Feature | SSE | WebSocket |
|---------|-----|-----------|
| Direction | Server → Client | Bidirectional |
| Protocol | HTTP | WebSocket upgrade |
| Auto-reconnect | Yes (built-in) | Manual |
| Browser support | Native EventSource | Native WebSocket |
| Proxy/CDN | Works transparently | May need configuration |
| Best for | LLM streaming, dashboards | Voice AI, games, chat |
Server-Sent Events is **the simplest and most practical technology for streaming LLM responses to web clients** — by building on standard HTTP without protocol upgrades, providing automatic reconnection, and requiring minimal server-side code, SSE delivers exactly the token-streaming capability that makes AI chat interfaces feel responsive while being dramatically simpler to implement and deploy than WebSocket-based alternatives.