@upstash/workflow 1.4.0-rc → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/astro.d.mts +18 -2
- package/astro.d.ts +18 -2
- package/astro.js +129 -29
- package/astro.mjs +1 -1
- package/{chunk-UH6O5IJY.mjs → chunk-J3PXPHSB.mjs} +129 -29
- package/cloudflare.d.mts +16 -2
- package/cloudflare.d.ts +16 -2
- package/cloudflare.js +129 -29
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +374 -0
- package/docs/agents/getting-started.mdx +139 -0
- package/docs/agents/overview.mdx +40 -0
- package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
- package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
- package/docs/agents/patterns/parallelization.mdx +77 -0
- package/docs/agents/patterns/prompt-chaining.mdx +84 -0
- package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
- package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
- package/docs/basics/best-practices/deterministic-code.mdx +132 -0
- package/docs/basics/best-practices/idempotency.mdx +38 -0
- package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
- package/docs/basics/best-practices/overview.mdx +35 -0
- package/docs/basics/best-practices/promise-any.mdx +9 -0
- package/docs/basics/best-practices/return-step-results.mdx +80 -0
- package/docs/basics/best-practices/workflow-limits.mdx +62 -0
- package/docs/basics/client/cancel.mdx +137 -0
- package/docs/basics/client/dlq/callback.mdx +21 -0
- package/docs/basics/client/dlq/delete.mdx +120 -0
- package/docs/basics/client/dlq/list.mdx +96 -0
- package/docs/basics/client/dlq/restart.mdx +164 -0
- package/docs/basics/client/dlq/resume.mdx +167 -0
- package/docs/basics/client/logs.mdx +139 -0
- package/docs/basics/client/notify.mdx +79 -0
- package/docs/basics/client/trigger.mdx +139 -0
- package/docs/basics/client/waiters.mdx +31 -0
- package/docs/basics/client.mdx +40 -0
- package/docs/basics/how.mdx +65 -0
- package/docs/basics/serve/advanced.mdx +435 -0
- package/docs/basics/serve.mdx +142 -0
- package/docs/changelog.mdx +174 -0
- package/docs/examples/allInOne.mdx +377 -0
- package/docs/examples/authWebhook.mdx +567 -0
- package/docs/examples/customRetry.mdx +309 -0
- package/docs/examples/customerOnboarding.mdx +230 -0
- package/docs/examples/dynamicWorkflow.mdx +203 -0
- package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
- package/docs/examples/imageProcessing.mdx +339 -0
- package/docs/examples/paymentRetry.mdx +438 -0
- package/docs/examples/waitForEvent.mdx +201 -0
- package/docs/features/dlq/callback.mdx +42 -0
- package/docs/features/dlq/delete.mdx +29 -0
- package/docs/features/dlq/restart.mdx +33 -0
- package/docs/features/dlq/resume.mdx +37 -0
- package/docs/features/dlq.mdx +39 -0
- package/docs/features/failure-callback.mdx +102 -0
- package/docs/features/failureFunction/advanced.mdx +36 -0
- package/docs/features/failureFunction/reliability.mdx +33 -0
- package/docs/features/flow-control/monitor.mdx +22 -0
- package/docs/features/flow-control/parallelism.mdx +45 -0
- package/docs/features/flow-control/rate-period.mdx +46 -0
- package/docs/features/flow-control.mdx +164 -0
- package/docs/features/invoke/serveMany.mdx +109 -0
- package/docs/features/invoke.mdx +39 -0
- package/docs/features/notify.mdx +127 -0
- package/docs/features/parallel-steps.mdx +41 -0
- package/docs/features/retries/prevent-retries.mdx +140 -0
- package/docs/features/retries.mdx +93 -0
- package/docs/features/wait-for-event.mdx +104 -0
- package/docs/features/wait.mdx +44 -0
- package/docs/features/webhooks.mdx +116 -0
- package/docs/getstarted.mdx +200 -0
- package/docs/howto/cancel.mdx +50 -0
- package/docs/howto/changes.mdx +33 -0
- package/docs/howto/configure.mdx +42 -0
- package/docs/howto/failures.mdx +185 -0
- package/docs/howto/get-token.mdx +18 -0
- package/docs/howto/local-development/development-server.mdx +130 -0
- package/docs/howto/local-development/local-tunnel.mdx +73 -0
- package/docs/howto/middlewares.mdx +263 -0
- package/docs/howto/migrations.mdx +335 -0
- package/docs/howto/multi-region.mdx +107 -0
- package/docs/howto/parallel-runs.mdx +52 -0
- package/docs/howto/readonly-token.mdx +19 -0
- package/docs/howto/realtime/basic.mdx +315 -0
- package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
- package/docs/howto/redact-fields.mdx +61 -0
- package/docs/howto/retry-now.mdx +41 -0
- package/docs/howto/schedule.mdx +104 -0
- package/docs/howto/security.mdx +143 -0
- package/docs/howto/singleton.mdx +137 -0
- package/docs/howto/start.mdx +69 -0
- package/docs/howto/use-webhooks.mdx +256 -0
- package/docs/integrations/aisdk.mdx +316 -0
- package/docs/integrations/anthropic.mdx +51 -0
- package/docs/integrations/datadog.mdx +69 -0
- package/docs/integrations/openai.mdx +87 -0
- package/docs/integrations/prometheus.mdx +65 -0
- package/docs/integrations/resend.mdx +82 -0
- package/docs/llms-txt.mdx +4 -0
- package/docs/openapi.yaml +2944 -0
- package/docs/pricing.mdx +22 -0
- package/docs/quickstarts/astro.mdx +185 -0
- package/docs/quickstarts/cloudflare-workers.mdx +190 -0
- package/docs/quickstarts/express.mdx +190 -0
- package/docs/quickstarts/fastapi.mdx +282 -0
- package/docs/quickstarts/flask.mdx +282 -0
- package/docs/quickstarts/hono.mdx +221 -0
- package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
- package/docs/quickstarts/nextjs-flask.mdx +147 -0
- package/docs/quickstarts/nuxt.mdx +169 -0
- package/docs/quickstarts/platforms.mdx +78 -0
- package/docs/quickstarts/solidjs.mdx +161 -0
- package/docs/quickstarts/svelte.mdx +170 -0
- package/docs/quickstarts/tanstack-start.mdx +205 -0
- package/docs/quickstarts/vercel-nextjs.mdx +233 -0
- package/docs/roadmap.mdx +8 -0
- package/docs/sdk/workflow-js.mdx +4 -0
- package/docs/sdk/workflow-py.mdx +4 -0
- package/docs/steps/api.mdx +64 -0
- package/docs/steps/call.mdx +183 -0
- package/docs/steps/cancel.mdx +25 -0
- package/docs/steps/createWebhook.mdx +40 -0
- package/docs/steps/invoke.mdx +100 -0
- package/docs/steps/notify.mdx +105 -0
- package/docs/steps/run.mdx +150 -0
- package/docs/steps/sleep.mdx +97 -0
- package/docs/steps/sleepUntil.mdx +88 -0
- package/docs/steps/waitForEvent.mdx +60 -0
- package/docs/steps/waitForWebhook.mdx +78 -0
- package/docs/steps.mdx +108 -0
- package/docs/troubleshooting/general.mdx +331 -0
- package/docs/troubleshooting/vercel.mdx +82 -0
- package/express.d.mts +18 -2
- package/express.d.ts +18 -2
- package/express.js +129 -29
- package/express.mjs +1 -1
- package/h3.d.mts +18 -2
- package/h3.d.ts +18 -2
- package/h3.js +129 -29
- package/h3.mjs +1 -1
- package/hono.d.mts +16 -2
- package/hono.d.ts +16 -2
- package/hono.js +129 -29
- package/hono.mjs +1 -1
- package/index.d.mts +49 -21
- package/index.d.ts +49 -21
- package/index.js +145 -41
- package/index.mjs +17 -13
- package/nextjs.d.mts +39 -2
- package/nextjs.d.ts +39 -2
- package/nextjs.js +129 -29
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +15 -2
- package/react-router.d.ts +15 -2
- package/react-router.js +129 -29
- package/react-router.mjs +1 -1
- package/{serve-many-CbIV7145.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-BCtZg31b.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +9 -1
- package/solidjs.d.ts +9 -1
- package/solidjs.js +129 -29
- package/solidjs.mjs +1 -1
- package/src/platforms/astro.ts +84 -0
- package/src/platforms/cloudflare.ts +127 -0
- package/src/platforms/express.ts +147 -0
- package/src/platforms/h3.ts +105 -0
- package/src/platforms/hono.ts +103 -0
- package/src/platforms/nextjs.ts +193 -0
- package/src/platforms/react-router.ts +92 -0
- package/src/platforms/solidjs.ts +59 -0
- package/src/platforms/svelte.ts +99 -0
- package/src/platforms/tanstack.ts +96 -0
- package/src/src/client/dlq.ts +387 -0
- package/src/src/client/filter-types.ts +166 -0
- package/src/src/client/index.ts +350 -0
- package/src/src/client/types.ts +501 -0
- package/src/src/client/utils.ts +307 -0
- package/src/src/constants.ts +82 -0
- package/src/src/context/api/anthropic.ts +58 -0
- package/src/src/context/api/base.ts +52 -0
- package/src/src/context/api/index.ts +24 -0
- package/src/src/context/api/openai.ts +125 -0
- package/src/src/context/api/resend.ts +57 -0
- package/src/src/context/auto-executor.ts +707 -0
- package/src/src/context/context.ts +681 -0
- package/src/src/context/index.ts +1 -0
- package/src/src/context/provider.ts +37 -0
- package/src/src/context/steps.ts +965 -0
- package/src/src/error.ts +203 -0
- package/src/src/index.ts +13 -0
- package/src/src/middleware/default-callbacks.ts +20 -0
- package/src/src/middleware/index.ts +2 -0
- package/src/src/middleware/logging.ts +39 -0
- package/src/src/middleware/manager.ts +148 -0
- package/src/src/middleware/middleware.ts +46 -0
- package/src/src/middleware/types.ts +72 -0
- package/src/src/qstash/headers.ts +397 -0
- package/src/src/qstash/step-config.ts +268 -0
- package/src/src/qstash/submit-steps.ts +284 -0
- package/src/src/serve/authorization.ts +118 -0
- package/src/src/serve/index.ts +475 -0
- package/src/src/serve/multi-region/handlers.ts +225 -0
- package/src/src/serve/multi-region/utils.ts +88 -0
- package/src/src/serve/options.ts +241 -0
- package/src/src/serve/serve-many.ts +117 -0
- package/src/src/serve/utils.ts +11 -0
- package/src/src/types.ts +590 -0
- package/src/src/utils.ts +141 -0
- package/src/src/workflow-parser.ts +472 -0
- package/src/src/workflow-requests.ts +575 -0
- package/svelte.d.mts +16 -2
- package/svelte.d.ts +16 -2
- package/svelte.js +129 -29
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +14 -2
- package/tanstack.d.ts +14 -2
- package/tanstack.js +129 -29
- package/tanstack.mjs +1 -1
- package/{types-ffLPVG5_.d.ts → types-CrgK5P6p.d.mts} +124 -24
- package/{types-ffLPVG5_.d.mts → types-CrgK5P6p.d.ts} +124 -24
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Realtime Quickstart"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
[**Upstash Realtime**](/realtime/overall/quickstart) lets you emit events from your workflow and subscribe to them in real-time on your frontend.
|
|
6
|
+
|
|
7
|
+
## How It Works
|
|
8
|
+
|
|
9
|
+
Upstash Realtime is powered by Upstash Redis and provides a clean, 100% type-safe API for publishing and subscribing to events:
|
|
10
|
+
|
|
11
|
+
- Your frontend can subscribe to events
|
|
12
|
+
- When you **emit** an event, it's instantly delivered to live subscribers on the frontend
|
|
13
|
+
- You can also replay events that happened in the past
|
|
14
|
+
|
|
15
|
+
This guide shows you how to integrate Upstash Workflow with Upstash Realtime to display real-time progress updates in your frontend.
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
### 1. Install Packages
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @upstash/workflow @upstash/realtime @upstash/redis zod
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 2. Configure Upstash Realtime
|
|
26
|
+
|
|
27
|
+
Create a Realtime instance in `lib/realtime.ts`:
|
|
28
|
+
|
|
29
|
+
```typescript title="lib/realtime.ts"
|
|
30
|
+
import { InferRealtimeEvents, Realtime } from "@upstash/realtime";
|
|
31
|
+
import { Redis } from "@upstash/redis";
|
|
32
|
+
import z from "zod/v4";
|
|
33
|
+
|
|
34
|
+
const redis = Redis.fromEnv();
|
|
35
|
+
|
|
36
|
+
const schema = {
|
|
37
|
+
workflow: {
|
|
38
|
+
runFinish: z.object({}),
|
|
39
|
+
stepFinish: z.object({
|
|
40
|
+
stepName: z.string(),
|
|
41
|
+
result: z.unknown().optional(),
|
|
42
|
+
}),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const realtime = new Realtime({ schema, redis });
|
|
47
|
+
export type RealtimeEvents = InferRealtimeEvents<typeof realtime>;
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 3. Create a Realtime Middleware
|
|
51
|
+
|
|
52
|
+
Create a custom middleware that will emit events to Realtime at `lib/middleware.ts`:
|
|
53
|
+
|
|
54
|
+
```typescript title="lib/middleware.ts"
|
|
55
|
+
import { WorkflowMiddleware } from "@upstash/workflow";
|
|
56
|
+
import { realtime } from "./realtime";
|
|
57
|
+
|
|
58
|
+
export const realtimeMiddleware = new WorkflowMiddleware({
|
|
59
|
+
name: "realtime-events",
|
|
60
|
+
callbacks: {
|
|
61
|
+
afterExecution: async ({ context, stepName, result }) => {
|
|
62
|
+
const channel = realtime.channel(context.workflowRunId);
|
|
63
|
+
await channel.emit("workflow.stepFinish", {
|
|
64
|
+
stepName,
|
|
65
|
+
result,
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
runCompleted: async ({ context }) => {
|
|
69
|
+
const channel = realtime.channel(context.workflowRunId);
|
|
70
|
+
await channel.emit("workflow.runFinish", {});
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Key points:**
|
|
77
|
+
|
|
78
|
+
- The `afterExecution` callback is triggered after each workflow step completes
|
|
79
|
+
- The `runCompleted` callback is triggered when the entire workflow finishes
|
|
80
|
+
- We use `context.workflowRunId` to create a unique channel for each workflow run
|
|
81
|
+
- Events are automatically emitted without needing to manually call emit inside your workflow steps
|
|
82
|
+
|
|
83
|
+
### 4. Create a Realtime Endpoint
|
|
84
|
+
|
|
85
|
+
Create an API route at `app/api/realtime/route.ts` to handle Realtime connections:
|
|
86
|
+
|
|
87
|
+
```typescript title="app/api/realtime/route.ts"
|
|
88
|
+
import { handle } from "@upstash/realtime";
|
|
89
|
+
import { realtime } from "@/lib/realtime";
|
|
90
|
+
|
|
91
|
+
export const GET = handle({ realtime });
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This endpoint enables Server-Sent Events (SSE) connections for real-time updates.
|
|
95
|
+
|
|
96
|
+
### 5. Add the Realtime Provider
|
|
97
|
+
|
|
98
|
+
Wrap your application in the `RealtimeProvider` by updating your root layout at `app/layout.tsx`:
|
|
99
|
+
|
|
100
|
+
```tsx title="app/layout.tsx"
|
|
101
|
+
"use client";
|
|
102
|
+
|
|
103
|
+
import { RealtimeProvider } from "@upstash/realtime/client";
|
|
104
|
+
|
|
105
|
+
export default function RootLayout({
|
|
106
|
+
children,
|
|
107
|
+
}: {
|
|
108
|
+
children: React.ReactNode;
|
|
109
|
+
}) {
|
|
110
|
+
return (
|
|
111
|
+
<html>
|
|
112
|
+
<body>
|
|
113
|
+
<RealtimeProvider>{children}</RealtimeProvider>
|
|
114
|
+
</body>
|
|
115
|
+
</html>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 6. Create a Typed Client Hook
|
|
121
|
+
|
|
122
|
+
Create a typed `useRealtime` hook at `lib/realtime-client.ts`:
|
|
123
|
+
|
|
124
|
+
```typescript title="lib/realtime-client.ts"
|
|
125
|
+
"use client";
|
|
126
|
+
|
|
127
|
+
import { createRealtime } from "@upstash/realtime/client";
|
|
128
|
+
import type { RealtimeEvents } from "./realtime";
|
|
129
|
+
|
|
130
|
+
export const { useRealtime } = createRealtime<RealtimeEvents>();
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Building the Workflow
|
|
136
|
+
|
|
137
|
+
### 1. Create the Workflow Endpoint
|
|
138
|
+
|
|
139
|
+
Create your workflow at `app/api/workflow/route.ts`:
|
|
140
|
+
|
|
141
|
+
```typescript title="app/api/workflow/route.ts"
|
|
142
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
143
|
+
import { realtimeMiddleware } from "@/lib/middleware";
|
|
144
|
+
|
|
145
|
+
type WorkflowPayload = {
|
|
146
|
+
userId: string;
|
|
147
|
+
action: string;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export const { POST } = serve<WorkflowPayload>(
|
|
151
|
+
async (context) => {
|
|
152
|
+
const { userId, action } = context.requestPayload;
|
|
153
|
+
|
|
154
|
+
await context.run("validate-data", async () => {
|
|
155
|
+
return { valid: true, userId, action };
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
await context.run("process-action", async () => {
|
|
159
|
+
// Your business logic here
|
|
160
|
+
return { processed: true, userId, action };
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return { success: true, workflowRunId: context.workflowRunId };
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
middlewares: [realtimeMiddleware],
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Key points:**
|
|
172
|
+
|
|
173
|
+
- Import the `realtimeMiddleware` from `@/lib/middleware`
|
|
174
|
+
- Pass the middleware in the `middlewares` array to the `serve` function
|
|
175
|
+
- The middleware automatically emits events after each step and when the workflow completes
|
|
176
|
+
|
|
177
|
+
### 2. Create a Trigger Endpoint
|
|
178
|
+
|
|
179
|
+
Create an endpoint to trigger workflows at `app/api/trigger/route.ts`:
|
|
180
|
+
|
|
181
|
+
```typescript title="app/api/trigger/route.ts"
|
|
182
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
183
|
+
import { Client } from "@upstash/workflow";
|
|
184
|
+
|
|
185
|
+
export const workflowClient = new Client({
|
|
186
|
+
token: process.env.QSTASH_TOKEN,
|
|
187
|
+
baseUrl: process.env.QSTASH_URL,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
export async function POST(request: NextRequest) {
|
|
191
|
+
const workflowUrl = `${request.nextUrl.origin}/api/workflow`;
|
|
192
|
+
|
|
193
|
+
const { workflowRunId } = await workflowClient.trigger({
|
|
194
|
+
url: workflowUrl,
|
|
195
|
+
body: {
|
|
196
|
+
userId: "user-123",
|
|
197
|
+
action: "process-data",
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
return NextResponse.json({ workflowRunId });
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## Building the Frontend
|
|
208
|
+
|
|
209
|
+
### 1. Create a Custom Hook
|
|
210
|
+
|
|
211
|
+
Create a React hook to manage the Realtime subscription at `hooks/useWorkflow.ts`:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
"use client";
|
|
215
|
+
|
|
216
|
+
import { useRealtime } from "@/lib/realtime-client";
|
|
217
|
+
import { useState, useCallback } from "react";
|
|
218
|
+
|
|
219
|
+
interface WorkflowStep {
|
|
220
|
+
stepName: string;
|
|
221
|
+
result?: unknown;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function useWorkflow() {
|
|
225
|
+
const [workflowRunId, setWorkflowRunId] = useState<string | null>(null);
|
|
226
|
+
const [steps, setSteps] = useState<WorkflowStep[]>([]);
|
|
227
|
+
const [isRunFinished, setIsRunFinished] = useState(false);
|
|
228
|
+
|
|
229
|
+
useRealtime({
|
|
230
|
+
enabled: Boolean(workflowRunId),
|
|
231
|
+
channels: workflowRunId ? [workflowRunId] : [],
|
|
232
|
+
events: ["workflow.stepFinish", "workflow.runFinish"],
|
|
233
|
+
onData({ event, data }) {
|
|
234
|
+
if (event === "workflow.stepFinish") {
|
|
235
|
+
setSteps((prev) => [...prev, data]);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (event === "workflow.runFinish") {
|
|
239
|
+
setIsRunFinished(true);
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const trigger = () => {
|
|
245
|
+
setSteps([]);
|
|
246
|
+
setIsRunFinished(false);
|
|
247
|
+
|
|
248
|
+
const response = await fetch("/api/trigger", {
|
|
249
|
+
method: "POST",
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const data = await response.json();
|
|
253
|
+
setWorkflowRunId(data.workflowRunId);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
trigger,
|
|
258
|
+
workflowRunId,
|
|
259
|
+
steps,
|
|
260
|
+
isRunFinished,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Key features:**
|
|
266
|
+
|
|
267
|
+
- Subscribe to multiple events using the `events` array: `["workflow.stepFinish", "workflow.runFinish"]`
|
|
268
|
+
- The hook manages both triggering the workflow and subscribing to updates
|
|
269
|
+
- Type-safe event handling with TypeScript
|
|
270
|
+
|
|
271
|
+
### 2. Use the Hook in Your Component
|
|
272
|
+
|
|
273
|
+
```tsx
|
|
274
|
+
"use client";
|
|
275
|
+
|
|
276
|
+
import { useWorkflow } from "@/hooks/useWorkflow";
|
|
277
|
+
|
|
278
|
+
export default function WorkflowPage() {
|
|
279
|
+
const { trigger, steps, isRunFinished } = useWorkflow();
|
|
280
|
+
|
|
281
|
+
return (
|
|
282
|
+
<div>
|
|
283
|
+
<button onClick={trigger}>Click to Trigger Workflow</button>
|
|
284
|
+
|
|
285
|
+
{isRunFinished && <p>✅ Workflow Finished!</p>}
|
|
286
|
+
|
|
287
|
+
<p>Workflow Steps:</p>
|
|
288
|
+
|
|
289
|
+
{steps.map((step, index) => (
|
|
290
|
+
<div key={index}>
|
|
291
|
+
<strong>{step.stepName}</strong>
|
|
292
|
+
{Boolean(step.result) && <span>: {JSON.stringify(step.result)}</span>}
|
|
293
|
+
</div>
|
|
294
|
+
))}
|
|
295
|
+
</div>
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## How It All Works Together
|
|
301
|
+
|
|
302
|
+
1. **User triggers workflow**: The frontend calls `/api/trigger`, which returns a `workflowRunId`
|
|
303
|
+
2. **Frontend subscribes**: Using the `workflowRunId`, the frontend subscribes to the Realtime channel
|
|
304
|
+
3. **Workflow executes**: The workflow runs as a background job, emitting events at each step
|
|
305
|
+
4. **Real-time updates**: As the workflow emits events, they're instantly delivered to the frontend via Server-Sent Events
|
|
306
|
+
|
|
307
|
+
## Full Example
|
|
308
|
+
|
|
309
|
+
For a complete working example with all steps, error handling, and UI components, check out the [Upstash Realtime example on GitHub](https://github.com/upstash/workflow-js/tree/main/examples/upstash-realtime).
|
|
310
|
+
|
|
311
|
+
## Next Steps
|
|
312
|
+
|
|
313
|
+
- Learn about [human-in-the-loop workflows with Realtime](./human-in-the-loop)
|
|
314
|
+
- Explore [Realtime features](/realtime/overall/quickstart)
|
|
315
|
+
- Check out [Workflow configuration options](/workflow/howto/configure)
|