Streaming
Server-Sent Events for real-time data streaming.
Overview
Picadabra uses Server-Sent Events (SSE) for streaming responses. The client returns an AsyncIterable that yields events as they arrive.
client.stream()
Stream task execution with real-time updates.
const = await .({
: 'generate',
: { : 'Hello, world!' },
})
for await (const of ) {
switch (.) {
case 'data':
.('Chunk:', .)
break
case 'error':
.('Error:', .)
break
case 'done':
.('Stream completed')
break
}
}Event Types
| Type | Description | Fields |
|---|---|---|
data | Incremental data chunk | id?, data |
error | Error occurred during stream | id?, error: string |
done | Stream completed successfully | id? |
Cancellation
Use AbortController to cancel a stream:
const = new ()
// Cancel after 5 seconds
(() => .(), 5000)
const = await .(
{ : 'generate', : { : 'Long task...' } },
{ : . },
)
try {
for await (const of ) {
.()
}
} catch () {
if ( instanceof && . === 'AbortError') {
.('Stream cancelled')
}
}Framework Integration
For React applications, see React Integration for TanStack Query integration with automatic state management.
How is this guide?