Guides
Stream Resumption
FluxyChat stream resumption lets users reconnect to in-progress AI responses after a page refresh, network drop, or tab switch without losing partial output.
Stream Resumption
FluxyChat stream resumption lets users reconnect to in-progress AI responses after a page refresh, network drop, or tab switch without losing partial output.
The Problem
When an AI agent streams a long response (e.g., a detailed code review), and the user refreshes the page or briefly loses connectivity, the stream breaks. Without resumption:
- The partial response is lost
- The user must re-trigger the agent
- Tokens already generated are wasted
How It Works
- Stream start: When an AI stream begins, the worker assigns it a
streamIdand persists chunks to D1 as they arrive - Disconnection: The WebSocket drops; the stream continues server-side
- Reconnection: The client reconnects and requests stream resume with the
streamId - Resume: The worker replays buffered chunks, then switches to live streaming for remaining content
- Completion: Stream finalizes normally; the full message is persisted
SDK Usage
import { useChat } from "@fluxy-chat/sdk";
const { messages, resumeStream } = useChat({ roomId, client });
// On reconnect, the SDK automatically attempts to resume active streams
// You can also manually trigger resume:
await resumeStream(streamId);Manual Stream Resume
// After reconnecting, check for active streams
const activeStreams = await client.getActiveStreams(roomId);
for (const stream of activeStreams) {
await client.resumeStream(stream.id);
}WebSocket Events
During stream resumption, the following events flow over WebSocket:
| Event | Description |
|---|---|
stream_start | New stream initiated with streamId |
stream_chunk | Text delta or tool call update |
stream_state | Full current state of the stream (sent on resume) |
stream_finish | Stream completed, final message persisted |
stream_error | Stream failed, partial content saved |
Worker Configuration
Stream resumption is enabled by default. Configure via environment variables:
STREAM_RESUMPTION_ENABLED=true # default: true
STREAM_RESUMPTION_TTL_SECONDS=300 # how long to keep stream state (default: 5 min)
STREAM_RESUMPTION_MAX_CONCURRENT=10 # max concurrent resumable streams per roomIntegration with Existing Features
- SSE fallback: Stream resumption works across transport fallbacks (WS → SSE → polling)
- Agent runtime: The
agent-runtime.jsmodule checks for resumable streams on agent start - Room DO: The Durable Object tracks
activeStreamsand sendsstreamStateon WebSocket reconnect - DevTools: The DevTools UI shows active streams and their resume status
See Also
- LLM Middleware Guide — Intercept and modify stream behavior
- Workflow Agent Guide — Durable agent execution that survives restarts