Home Knowledge Base Server-Sent Events (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?

" whenever it has updates to deliver.

Why SSE Matters for AI/ML

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

FeatureSSEWebSocket
DirectionServer → ClientBidirectional
ProtocolHTTPWebSocket upgrade
Auto-reconnectYes (built-in)Manual
Browser supportNative EventSourceNative WebSocket
Proxy/CDNWorks transparentlyMay need configuration
Best forLLM streaming, dashboardsVoice 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.

sseserver sentstreaming

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.