Skip to content

Commit f807f1a

Browse files
committed
moved metadata task examples to metadata
1 parent f5da2f5 commit f807f1a

File tree

4 files changed

+106
-107
lines changed

4 files changed

+106
-107
lines changed

docs/realtime/backend/subscribe.mdx

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ async function monitorProgress(runId: string) {
192192
}
193193
```
194194

195-
For more information on how to write tasks that use the metadata API, see our [run metadata docs](/runs/metadata).
195+
For more information on how to write tasks that use the metadata API, as well as more examples, see our [run metadata docs](/runs/metadata#more-metadata-task-examples).
196196

197197
### Type safety
198198

@@ -223,99 +223,3 @@ async function monitorTypedProgress(runId: string) {
223223
}
224224
}
225225
```
226-
227-
### Common metadata patterns
228-
229-
#### Progress tracking
230-
231-
Track progress with percentage and current step:
232-
233-
```ts
234-
import { task, metadata } from "@trigger.dev/sdk/v3";
235-
236-
export const batchProcessingTask = task({
237-
id: "batch-processing",
238-
run: async (payload: { records: any[] }) => {
239-
for (let i = 0; i < payload.records.length; i++) {
240-
const record = payload.records[i];
241-
242-
// Update progress
243-
metadata.set("progress", {
244-
step: i + 1,
245-
total: payload.records.length,
246-
percentage: Math.round(((i + 1) / payload.records.length) * 100),
247-
currentRecord: record.id,
248-
});
249-
250-
await processRecord(record);
251-
}
252-
},
253-
});
254-
```
255-
256-
#### Status updates with logs
257-
258-
Append log entries while maintaining status:
259-
260-
```ts
261-
import { task, metadata } from "@trigger.dev/sdk/v3";
262-
263-
export const deploymentTask = task({
264-
id: "deployment",
265-
run: async (payload: { version: string }) => {
266-
metadata.set("status", "initializing");
267-
metadata.append("logs", "Starting deployment...");
268-
269-
// Step 1
270-
metadata.set("status", "building");
271-
metadata.append("logs", "Building application...");
272-
await buildApplication();
273-
274-
// Step 2
275-
metadata.set("status", "deploying");
276-
metadata.append("logs", "Deploying to production...");
277-
await deployToProduction();
278-
279-
// Step 3
280-
metadata.set("status", "verifying");
281-
metadata.append("logs", "Running health checks...");
282-
await runHealthChecks();
283-
284-
metadata.set("status", "completed");
285-
metadata.append("logs", "Deployment successful!");
286-
},
287-
});
288-
```
289-
290-
#### User context and notifications
291-
292-
Store user information and notification preferences:
293-
294-
```ts
295-
import { task, metadata } from "@trigger.dev/sdk/v3";
296-
297-
export const userTask = task({
298-
id: "user-task",
299-
run: async (payload: { userId: string; action: string }) => {
300-
// Set user context in metadata
301-
metadata.set("user", {
302-
id: payload.userId,
303-
action: payload.action,
304-
startedAt: new Date().toISOString(),
305-
});
306-
307-
// Update status for user notifications
308-
metadata.set("notification", {
309-
type: "info",
310-
message: `Starting ${payload.action} for user ${payload.userId}`,
311-
});
312-
313-
await performUserAction(payload);
314-
315-
metadata.set("notification", {
316-
type: "success",
317-
message: `${payload.action} completed successfully`,
318-
});
319-
},
320-
});
321-
```

docs/realtime/overview.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ import RealtimeExamplesCards from "/snippets/realtime-examples-cards.mdx";
88

99
Trigger.dev Realtime allows you to trigger, subscribe to, and get real-time updates for runs. This is useful for monitoring runs, updating UIs, and building real-time dashboards.
1010

11-
## Authentication
12-
13-
All Realtime subscriptions require authentication so you can securely trigger and subscribe to runs. See our [authentication guide](/realtime/auth) for more details.
14-
1511
## What you can do with Realtime
1612

17-
You can subscribe to real-time updates for different scopes of runs:
13+
Subscribe to real-time updates for different scopes of runs:
1814

1915
- **Specific runs** - Monitor individual run progress by run ID
2016
- **Runs with specific tags** - Track all runs that have certain [tags](/tags) (e.g., all runs tagged with `user:123`)
@@ -28,6 +24,10 @@ Once subscribed, you'll receive the complete [run object](/realtime/run-object)
2824
- **Tag changes** - When [tags](/tags) are added or removed from the run
2925
- **Stream data** - Real-time data chunks from your tasks, perfect for AI/LLM streaming ([React hooks](/realtime/react-hooks/streams) | [backend](/realtime/backend/streams))
3026

27+
## Authentication
28+
29+
All Realtime subscriptions require authentication so you can securely trigger and subscribe to runs. See our [authentication guide](/realtime/auth) for more details.
30+
3131
## Realtime approaches
3232

3333
### Frontend implementation

docs/realtime/react-hooks/subscribe.mdx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,11 @@ export function MyComponent({ batchId }: { batchId: string }) {
196196

197197
See our [Realtime documentation](/realtime) for more information.
198198

199-
## Using Metadata
199+
## Using metadata to show progress in your UI
200200

201201
All realtime hooks automatically include metadata updates. Whenever your task updates metadata using `metadata.set()`, `metadata.append()`, or other metadata methods, your component will re-render with the updated data.
202202

203-
<Note>
204-
To learn how to write tasks with metadata updates, see our [writing tasks using
205-
metadata](/runs/metadata) guide.
206-
</Note>
203+
<Note>To learn how to write tasks using metadata, see our [metadata](/runs/metadata) guide.</Note>
207204

208205
### Progress monitoring
209206

docs/runs/metadata.mdx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,104 @@ Combined with [Realtime](/realtime), you could use this to show a live progress
514514
height="100%"
515515
/>
516516

517+
## More metadata task examples
518+
519+
Using metadata updates in conjunction with our [Realtime React hooks](/realtime/react-hooks/overview) can be a powerful way to build real-time UIs. Here are some example tasks demonstrating how to use metadata in your tasks to track progress, status, and more:
520+
521+
### Progress tracking
522+
523+
Track progress with percentage and current step:
524+
525+
```ts
526+
import { task, metadata } from "@trigger.dev/sdk/v3";
527+
528+
export const batchProcessingTask = task({
529+
id: "batch-processing",
530+
run: async (payload: { records: any[] }) => {
531+
for (let i = 0; i < payload.records.length; i++) {
532+
const record = payload.records[i];
533+
534+
// Update progress
535+
metadata.set("progress", {
536+
step: i + 1,
537+
total: payload.records.length,
538+
percentage: Math.round(((i + 1) / payload.records.length) * 100),
539+
currentRecord: record.id,
540+
});
541+
542+
await processRecord(record);
543+
}
544+
},
545+
});
546+
```
547+
548+
### Status updates with logs
549+
550+
Append log entries while maintaining status:
551+
552+
```ts
553+
import { task, metadata } from "@trigger.dev/sdk/v3";
554+
555+
export const deploymentTask = task({
556+
id: "deployment",
557+
run: async (payload: { version: string }) => {
558+
metadata.set("status", "initializing");
559+
metadata.append("logs", "Starting deployment...");
560+
561+
// Step 1
562+
metadata.set("status", "building");
563+
metadata.append("logs", "Building application...");
564+
await buildApplication();
565+
566+
// Step 2
567+
metadata.set("status", "deploying");
568+
metadata.append("logs", "Deploying to production...");
569+
await deployToProduction();
570+
571+
// Step 3
572+
metadata.set("status", "verifying");
573+
metadata.append("logs", "Running health checks...");
574+
await runHealthChecks();
575+
576+
metadata.set("status", "completed");
577+
metadata.append("logs", "Deployment successful!");
578+
},
579+
});
580+
```
581+
582+
### User context and notifications
583+
584+
Store user information and notification preferences:
585+
586+
```ts
587+
import { task, metadata } from "@trigger.dev/sdk/v3";
588+
589+
export const userTask = task({
590+
id: "user-task",
591+
run: async (payload: { userId: string; action: string }) => {
592+
// Set user context in metadata
593+
metadata.set("user", {
594+
id: payload.userId,
595+
action: payload.action,
596+
startedAt: new Date().toISOString(),
597+
});
598+
599+
// Update status for user notifications
600+
metadata.set("notification", {
601+
type: "info",
602+
message: `Starting ${payload.action} for user ${payload.userId}`,
603+
});
604+
605+
await performUserAction(payload);
606+
607+
metadata.set("notification", {
608+
type: "success",
609+
message: `${payload.action} completed successfully`,
610+
});
611+
},
612+
});
613+
```
614+
517615
## Metadata propagation
518616

519617
Metadata is NOT propagated to child tasks. If you want to pass metadata to a child task, you must do so explicitly:

0 commit comments

Comments
 (0)