Chat - Streaming lifecycle
Observe the full runtime lifecycle for send, stream, stop, error, retry, and callbacks.
This demo brings the runtime lifecycle together in one place. It intentionally exposes success, failure, and long-running stream modes so you can observe every lifecycle phase:
sendMessage()— send a user turn and start streamingstopStreaming()— cancel an active streamretry()— retry a failed or cancelled messageonData— fires when adata-*chunk arrivesonFinish— fires when the stream ends (success, abort, or error)onError— fires on any runtime error
Key concepts
The send-stream flow
When sendMessage() is called:
- The user message is added optimistically with
status: 'sending' - The adapter's
sendMessage()returns aReadableStream - Chunks update the assistant message in real time
- A
finishchunk completes the turn — user message moves tostatus: 'sent'
Stopping a stream
Call stopStreaming() to abort the active stream.
The user message moves to status: 'cancelled' and the onFinish callback fires with isAbort: true.
Retrying a failed message
When a stream fails or is cancelled, call retry(messageId) with the user message ID.
The runtime removes the failed assistant messages and resends the user turn.
Runtime callbacks
Register callbacks on ChatProvider to observe lifecycle events:
<ChatProvider
adapter={adapter}
onData={(part) => console.log('data chunk', part.type)}
onFinish={(payload) => console.log('finished', payload.finishReason)}
onError={(error) => console.log('error', error.message)}
>
<MyChat />
</ChatProvider>
Streaming lifecycle
Callback events will appear here.
No messages yet.
Key takeaways
- The runtime manages the full send → stream → finish lifecycle automatically
stopStreaming()aborts the stream and triggersonFinishwithisAbort: trueretry()removes failed assistant messages and resends the original user turn- Callbacks (
onData,onFinish,onError) provide lifecycle observability without modifying the store
See also
- Streaming for the full chunk protocol reference
- State and store for the error model and callback signatures
- Message parts for rendering multi-part responses
- Tool call events for tool-specific lifecycle callbacks