Realtime overview
Using the Trigger.dev v3 realtime API
Trigger.dev Realtime is a set of APIs that allow you to subscribe to runs and get real-time updates on the run status. This is useful for monitoring runs, updating UIs, and building realtime dashboards.
How it works
The Realtime API is built on top of Electric SQL, an open-source PostgreSQL syncing engine. The Trigger.dev API wraps Electric SQL and provides a simple API to subscribe to runs and get real-time updates.
Walkthrough
Usage
After you trigger a task, you can subscribe to the run using the runs.subscribeToRun
function. This function returns an async iterator that you can use to get updates on the run status.
Every time the run changes, the async iterator will yield the updated run. You can use this to update your UI, log the run status, or take any other action.
Alternatively, you can subscribe to changes to any run that includes a specific tag (or tags) using the runs.subscribeToRunsWithTag
function.
If you’ve used batchTrigger
to trigger multiple runs, you can also subscribe to changes to all the runs triggered in the batch using the runs.subscribeToBatch
function.
React hooks
We also provide a set of React hooks that make it easy to use the Realtime API in your React components. See the React hooks doc for more information.
Run changes
You will receive updates whenever a run changes for the following reasons:
- The run moves to a new state. See our run lifecycle docs for more information.
- Run tags are added or removed.
- Run metadata is updated.
Run object
The run object returned by the async iterator is NOT the same as the run object returned by the runs.retrieve
function. This is because Electric SQL streams changes from a single PostgreSQL table, and the run object returned by runs.retrieve
is a combination of multiple tables.
The run object returned by the async iterator has the following fields:
The run ID.
The task identifier.
The input payload for the run.
The output result of the run.
Timestamp when the run was created.
Timestamp when the run was last updated.
Sequential number assigned to the run.
Current status of the run.
Duration of the run in milliseconds.
Total cost of the run in cents.
Base cost of the run in cents before any additional charges.
Array of tags associated with the run.
Key used to ensure idempotent execution.
Timestamp when the run expired.
Time-to-live duration for the run.
Timestamp when the run finished.
Timestamp when the run started.
Timestamp until which the run is delayed.
Timestamp when the run was queued.
Additional metadata associated with the run.
Error information if the run failed.
Indicates whether this is a test run.
Type-safety
You can infer the types of the run’s payload and output by passing the type of the task to the subscribeToRun
function. This will give you type-safe access to the run’s payload and output.
When using subscribeToRunsWithTag
, you can pass a union of task types for all the possible tasks that can have the tag.
Run metadata
The run metadata API gives you the ability to add or update custom metadata on a run, which will cause the run to be updated. This allows you to extend the realtime API with custom data attached to a run that can be used for various purposes. Some common use cases include:
- Adding a link to a related resource
- Adding a reference to a user or organization
- Adding a custom status with progress information
See our run metadata docs for more on how to use this feature.
Using w/Realtime & React hooks
We suggest combining run metadata with the realtime API and our React hooks to bridge the gap between your trigger.dev tasks and your UI. This allows you to update your UI in real-time based on changes to the run metadata. As a simple example, you could add a custom status to a run with a progress value, and update your UI based on that progress.
We have a full demo app repo available here
Limits
The Realtime API in the Trigger.dev Cloud limits the number of concurrent subscriptions, depending on your plan. If you exceed the limit, you will receive an error when trying to subscribe to a run. For more information, see our pricing page.
Was this page helpful?