Skip to content

Commit 8849168

Browse files
committed
Updated how it works
1 parent b6d97e7 commit 8849168

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

docs/realtime/how-it-works.mdx

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: How it works
33
sidebarTitle: How it works
4-
description: Technical details about the Trigger.dev Realtime API
4+
description: Technical details about how the Trigger.dev Realtime API works
55
---
66

77
## Architecture
@@ -18,74 +18,75 @@ You will receive updates whenever a run changes for the following reasons:
1818

1919
## Run object
2020

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.
2222

23-
## Type-safety
23+
## Basic usage
2424

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.
2626

2727
```ts
2828
import { runs, tasks } from "@trigger.dev/sdk/v3";
29-
import type { myTask } from "./trigger/my-task";
3029

3130
// Somewhere in your backend code
3231
async function myBackend() {
3332
const handle = await tasks.trigger("my-task", { some: "data" });
3433

35-
for await (const run of runs.subscribeToRun<typeof myTask>(handle.id)) {
34+
for await (const run of runs.subscribeToRun(handle.id)) {
3635
// 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);
4337
}
4438
}
4539
```
4640

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.
4844

4945
```ts
5046
import { runs } from "@trigger.dev/sdk/v3";
51-
import type { myTask, myOtherTask } from "./trigger/my-task";
5247

5348
// Somewhere in your backend code
54-
for await (const run of runs.subscribeToRunsWithTag<typeof myTask | typeof myOtherTask>("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+
for await (const run of runs.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+
for await (const run of runs.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);
6664
}
6765
```
6866

6967
## Run metadata
7068

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:
7270

7371
- Adding a link to a related resource
7472
- Adding a reference to a user or organization
7573
- Adding a custom status with progress information
7674

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.
7876

79-
### Using w/Realtime & React hooks
77+
### Using metadata with Realtime & React hooks
8078

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:
8280

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.
8483

8584
## Limits
8685

8786
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).
8887

89-
## Known issues
88+
## Learn more
9089

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

Comments
 (0)