In this article
Context
Our AI chat and content generation features need to stream LLM responses to users rather than waiting for full completion. We need to choose between Server-Sent Events (SSE), WebSockets, and polling for delivering streaming tokens from the LLM to the browser.
Options Evaluated
Server-Sent Events (SSE) via Vercel AI SDK
Pros
- +Native browser support — no library needed
- +Vercel AI SDK provides toDataStreamResponse() for seamless SSE streaming
- +HTTP/1.1 compatible — works through proxies and firewalls
- +Automatic reconnection built into the EventSource API
- +One-directional (server → client) is all we need for token streaming
Cons
- −One-directional only — cannot send messages back to server mid-stream
- −Limited to 6 concurrent connections in HTTP/1.1 (HTTP/2 removes this limit)
WebSocket
Pros
- +Bidirectional — useful if user needs to send corrections mid-stream
- +Lower overhead per message than HTTP for long-lived connections
Cons
- −Overkill for unidirectional token streaming
- −More complex to manage connection lifecycle, reconnection, and error handling
- −Vercel Edge Functions have limited WebSocket support
- −Not needed in our current use cases
Long-polling
Pros
- +Simplest implementation — standard HTTP
- +Works everywhere
Cons
- −High server load — each poll is a new HTTP request
- −Noticeable latency between token batches
- −Not suitable for <100ms token delivery
Decision
We chose SSE via the Vercel AI SDK. The toDataStreamResponse() pattern handles all the streaming mechanics, and the useChat/useCompletion hooks on the client consume the stream cleanly. HTTP/2 on Vercel eliminates the 6-connection limit. SSE meets all our current requirements at the lowest complexity.
Consequences
- •All LLM route handlers use streamText + toDataStreamResponse()
- •Client components use useChat() or useCompletion() hooks from @ai-sdk/react
- •Implement stream abort support so users can stop generation early
- •Add streaming safety check: buffer 200-token sliding window for mid-stream content safety

