Streaming in Web Applications
What you'll learn: How to connect server-sent streaming responses to live frontend updates using async patterns and event handlers.
The Core Concept
Streaming from an LLM backend to a web UI means displaying tokens as they arrive—like a typewriter effect—rather than waiting for the complete response. On the server, you use async generators (Python) to yield chunks as they come from the API. On the frontend, you use event handlers (JavaScript) to receive and display each chunk immediately.
This creates the responsive, ChatGPT-like experience users expect.
Backend: Async Generators
Your server endpoint wraps the streaming API call in an async generator:
async def stream_response(prompt):
async for chunk in openai_client.stream(prompt):
yield chunk # Send each delta to the client
This yields Server-Sent Events (SSE) to the browser as they arrive.
Frontend: Event Handlers
Your JavaScript listens for incoming events and updates the DOM:
const eventSource = new EventSource('/api/stream');
eventSource.onmessage = (event) => {
const chunk = event.data;
document.getElementById('output').innerText += chunk; // Append token
};
eventSource.onerror = () => {
eventSource.close(); // Clean up on completion
};
Each onmessage fires when a new token arrives, letting you append it to the UI instantly.
Why This Matters
Without streaming integration, users see a blank screen until the full response finishes (which can take 30+ seconds). With streaming, they see progress immediately, reducing perceived latency and improving UX dramatically.
Key Takeaway: Streaming in web apps combines backend async generators that yield chunks and frontend event handlers that render them live—bridging the gap between API streams and user-facing interfaces.