Realtime hooks
Get live updates from the Trigger.dev API in your frontend application.
These hooks allow you to subscribe to runs, batches, and streams using Trigger.dev realtime. Before reading this guide:
- Read our Realtime documentation to understand how the Trigger.dev realtime API works.
- Read how to setup and authenticate using the
@trigger.dev/react-hooks
package.
Hooks
useRealtimeRun
The useRealtimeRun
hook allows you to subscribe to a run by its ID.
To correctly type the run’s payload and output, you can provide the type of your task to the useRealtimeRun
hook:
You can supply an onComplete
callback to the useRealtimeRun
hook to be called when the run is completed or errored. This is useful if you want to perform some action when the run is completed, like navigating to a different page or showing a notification.
See our Realtime documentation for more information about the type of the run object and more.
useRealtimeRunsWithTag
The useRealtimeRunsWithTag
hook allows you to subscribe to multiple runs with a specific tag.
To correctly type the runs payload and output, you can provide the type of your task to the useRealtimeRunsWithTag
hook:
If useRealtimeRunsWithTag
could return multiple different types of tasks, you can pass a union of all the task types to the hook:
useRealtimeBatch
The useRealtimeBatch
hook allows you to subscribe to a batch of runs by its the batch ID.
See our Realtime documentation for more information.
useRealtimeRunWithStreams
The useRealtimeRunWithStreams
hook allows you to subscribe to a run by its ID and also receive any streams that are emitted by the task. See our Realtime documentation for more information about emitting streams from a task.
You can provide the type of the streams to the useRealtimeRunWithStreams
hook:
As you can see above, each stream is an array of the type you provided, keyed by the stream name. If instead of a pure text stream you have a stream of objects, you can provide the type of the object:
Common options
accessToken & baseURL
You can pass the accessToken
option to the Realtime hooks to authenticate the subscription.
enabled
You can pass the enabled
option to the Realtime hooks to enable or disable the subscription.
This allows you to conditionally disable using the hook based on some state.
id
You can pass the id
option to the Realtime hooks to change the ID of the subscription.
This allows you to change the ID of the subscription based on some state. Passing in a different ID will unsubscribe from the current subscription and subscribe to the new one (and remove any cached data).
experimental_throttleInMs
The *withStreams
variants of the Realtime hooks accept 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.
Was this page helpful?