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 theruns.subscribeToRun function. This function returns an async iterator that you can use to get updates on the run status.
runs.subscribeToRunsWithTag function.
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 theruns.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:
string
required
The run ID.
string
required
The task identifier.
object
required
The input payload for the run.
object
The output result of the run.
Date
required
Timestamp when the run was created.
Date
required
Timestamp when the run was last updated.
number
required
Sequential number assigned to the run.
RunStatus
required
Current status of the run.
RunStatus enum
RunStatus enum
number
required
Duration of the run in milliseconds.
number
required
Total cost of the run in cents.
number
required
Base cost of the run in cents before any additional charges.
string[]
required
Array of tags associated with the run.
string
Key used to ensure idempotent execution.
Date
Timestamp when the run expired.
string
Time-to-live duration for the run.
Date
Timestamp when the run finished.
Date
Timestamp when the run started.
Date
Timestamp until which the run is delayed.
Date
Timestamp when the run was queued.
Record<string, DeserializedJson>
Additional metadata associated with the run.
SerializedError
Error information if the run failed.
boolean
required
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 thesubscribeToRun function. This will give you type-safe access to the run’s payload and output.
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

