Server-Sent Events (SSE) Fundamentals
What you'll learn: How SSE enables real-time streaming of AI responses from server to client, making chatbot experiences feel instant and interactive.
The Problem SSE Solves
When you use ChatGPT or Claude, you see words appearing one by one rather than waiting for the entire response. That's streaming in action. Without SSE, your app would make a request and wait (sometimes 30+ seconds) for the complete answer. SSE lets the server push data to your client continuously over a single, long-lived HTTP connection.
How SSE Works
Think of SSE like a news ticker on TV. Once you tune in (open the connection), updates keep flowing to you without asking for each one. The server keeps the connection open and sends chunks of data as they become available.
SSE uses a simple text-based protocol. The server sends messages in this format:
data: {"choices":[{"delta":{"content":"Hello"}}]}
data: {"choices":[{"delta":{"content":" world"}}]}
data: [DONE]
Each message starts with data:, followed by the content. A blank line separates messages. The client listens and processes each chunk as it arrives.
Parsing LLM API Event Streams
When you set stream=true in your LLM API request (from lessons 85-89), you receive SSE responses. Your client needs to:
- Keep the connection open
- Read each
data:line - Parse the JSON payload
- Extract the text fragment (usually in
delta.content) - Accumulate fragments into the full response
- Stop when receiving the
[DONE]signal
Key Takeaway: SSE transforms slow, blocking API calls into responsive, real-time experiences by streaming text chunks as the LLM generates them, using a simple event-based protocol your client parses line-by-line.