You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Technical details about the Trigger.dev Realtime API
4
+
description: Technical details about how the Trigger.dev Realtime API works
5
5
---
6
6
7
7
## Architecture
@@ -18,74 +18,75 @@ You will receive updates whenever a run changes for the following reasons:
18
18
19
19
## Run object
20
20
21
-
The run object returned by realtime subscriptions is optimized for streaming updates and differs from the management API's run object. See the [run object reference](/realtime/run-object) for the complete schema and field descriptions.
21
+
The run object returned by Realtime subscriptions is optimized for streaming updates and differs from the management API's run object. See [the run object](/realtime/run-object) page for the complete schema and field descriptions.
22
22
23
-
## Type-safety
23
+
## Basic usage
24
24
25
-
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.
25
+
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.
forawait (const run ofruns.subscribeToRun<typeofmyTask>(handle.id)) {
34
+
forawait (const run ofruns.subscribeToRun(handle.id)) {
36
35
// This will log the run every time it changes
37
-
console.log(run.payload.some);
38
-
39
-
if (run.output) {
40
-
// This will log the output if it exists
41
-
console.log(run.output.some);
42
-
}
36
+
console.log(run);
43
37
}
44
38
}
45
39
```
46
40
47
-
When using `subscribeToRunsWithTag`, you can pass a union of task types for all the possible tasks that can have the tag.
41
+
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.
42
+
43
+
Alternatively, you can subscribe to changes to any run that includes a specific tag (or tags) using the `runs.subscribeToRunsWithTag` function.
forawait (const run ofruns.subscribeToRunsWithTag<typeofmyTask|typeofmyOtherTask>("my-tag")) {
55
-
// You can narrow down the type based on the taskIdentifier
56
-
switch (run.taskIdentifier) {
57
-
case"my-task": {
58
-
console.log("Run output:", run.output.foo); // This will be type-safe
59
-
break;
60
-
}
61
-
case"my-other-task": {
62
-
console.log("Run output:", run.output.bar); // This will be type-safe
63
-
break;
64
-
}
65
-
}
49
+
forawait (const run ofruns.subscribeToRunsWithTag("user:1234")) {
50
+
// This will log the run every time it changes, for all runs with the tag "user:1234"
51
+
console.log(run);
52
+
}
53
+
```
54
+
55
+
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.
56
+
57
+
```ts
58
+
import { runs } from"@trigger.dev/sdk/v3";
59
+
60
+
// Somewhere in your backend code
61
+
forawait (const run ofruns.subscribeToBatch("batch-id")) {
62
+
// This will log the run every time it changes, for all runs in the batch with the ID "batch-id"
63
+
console.log(run);
66
64
}
67
65
```
68
66
69
67
## Run metadata
70
68
71
-
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:
69
+
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:
72
70
73
71
- Adding a link to a related resource
74
72
- Adding a reference to a user or organization
75
73
- Adding a custom status with progress information
76
74
77
-
See our [run metadata docs](/runs/metadata) for more on how to use this feature.
75
+
See our [run metadata docs](/runs/metadata) for more on how to write tasks that use the metadata API.
78
76
79
-
### Using w/Realtime & React hooks
77
+
### Using metadata with Realtime & React hooks
80
78
81
-
We suggest combining run metadata with the realtime API and our [React hooks](/realtime/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.
79
+
You can combine run metadata with the Realtime API to bridge the gap between your trigger.dev tasks and your applications in two ways:
82
80
83
-
We have a full demo app repo available [here](https://github.com/triggerdotdev/nextjs-realtime-simple-demo)
81
+
1. Using our [React hooks](/realtime/react-hooks/subscribe#using-metadata) to subscribe to metadata updates and update your UI in real-time.
82
+
2. Using our [backend functions](/realtime/backend) to subscribe to metadata updates in your backend.
84
83
85
84
## Limits
86
85
87
86
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](https://trigger.dev/pricing).
88
87
89
-
## Known issues
88
+
## Learn more
90
89
91
-
There is currently a known issue where the realtime API does not work if subscribing to a run that has a large payload or large output and are stored in object store instead of the database. We are working on a fix for this issue: https://github.com/triggerdotdev/trigger.dev/issues/1451. As a workaround you'll need to keep payloads and outputs below 128KB when using the realtime API.
90
+
- Read our Realtime blog post ["How we built a real-time service that handles 20,000 updates per second](https://trigger.dev/blog/how-we-built-realtime)
91
+
- Using Realtime: [React Hooks (frontend)](/realtime/react-hooks)
92
+
- Using [Backend (server-side)](/realtime/backend)
0 commit comments