To learn how to emit streams from your tasks, see Streaming data from tasks.
useRealtimeStream (Recommended)
Available in SDK version 4.1.0 or later. This is the recommended way to consume streams in
your React components.
useRealtimeStream hook allows you to subscribe to a specific stream by its run ID and stream key. This hook is designed to work seamlessly with defined streams for full type safety.
Basic Usage
With Defined Streams
The recommended approach is to use defined streams for full type safety:Streaming AI Responses
Here’s a complete example showing how to display streaming AI responses:Options
TheuseRealtimeStream hook accepts the following options:
Using Default Stream
You can omit the stream key to use the default stream:useInputStreamSend
TheuseInputStreamSend hook lets you send data from your frontend into a running task’s input stream. Use it for cancel buttons, approval forms, or any UI that needs to push typed data into a running task.
Basic usage
Pass the input stream’sid (string), the run ID, and options such as accessToken. You typically get runId and accessToken from the object returned when you trigger the task (e.g. handle.id, handle.publicAccessToken). The hook returns send, isLoading, error, and isReady:
Options and return value
streamId: The input stream identifier (string). Use theidfrom your defined stream (e.g.approval.id) or the same string you used instreams.input<T>({ id: "approval" }).runId: The run to send input to. WhenrunIdis undefined,isReadyis false andsendwill not trigger.options:accessToken(required for client usage),baseURL(optional). See Realtime auth for generating a public access token with the right scopes (e.g. input streams write for that run).
send(data): Sends typed data to the input stream. Uses SWR mutation under the hood.isLoading: True while a send is in progress.error: Set if the last send failed.isReady: True when bothrunIdand access token are available.
.wait(), .once(), .on()), see Input Streams in the Streams doc.
useRealtimeRunWithStreams
For new projects, we recommend using
useRealtimeStream instead (available in SDK 4.1.0+). This
hook is still supported for backward compatibility and use cases where you need to subscribe to
both the run and all its streams at once.useRealtimeRunWithStreams hook allows you to subscribe to a run by its ID and also receive any streams that are emitted by the task. This is useful when you need to access both the run metadata and multiple streams simultaneously.
useRealtimeRunWithStreams hook to get type-safety:
Streaming AI responses with useRealtimeRunWithStreams
Here’s an example showing how to display streaming OpenAI responses usinguseRealtimeRunWithStreams:
AI SDK with tools
When using the AI SDK with tools withuseRealtimeRunWithStreams, you can access tool calls and results:
Throttling updates
TheuseRealtimeRunWithStreams hook accepts an experimental_throttleInMs option to throttle the updates from the server. This can be useful if you are getting too many updates and want to reduce the number of updates.
useRealtimeStream hook, use the throttleInMs option instead (see options above).
Frequently asked questions
How do I stream AI/LLM responses from a background task to React?
Define a typed stream in your task withstreams.define<string>(), pipe your AI SDK response to it with .pipe(), then consume it in your component with useRealtimeStream. See Streaming data from tasks for the task-side setup.
What’s the difference between streaming and run updates?
Run updates track run state (status, metadata, tags). Streaming (this page) pipes continuous data your task produces. Use run updates for progress bars and status badges. Use streaming for AI chat output, live logs, or file processing results. You can use both at the same time.Can I send data back into a running task from React?
Yes. Use theuseInputStreamSend hook to send data into a running task’s input stream. This is useful for cancel buttons, user approvals, or any interactive flow. See Input Streams for the full guide.
Do streams work with the Vercel AI SDK?
Yes. You can pipe a Vercel AI SDKstreamText response directly into a Trigger.dev stream using .pipe(). The Streaming data from tasks page has a complete AI streaming example.
