-
Notifications
You must be signed in to change notification settings - Fork 16.7k
[Workflows] add subscribe documentation and changelog #33361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mkuritsu
wants to merge
4
commits into
cloudflare:production
Choose a base branch
from
mkuritsu:rcorreia/add-workflows-subscription
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/content/changelog/workflows/2026-09-11-instance-event-subscriptions.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| --- | ||
| title: Stream Workflow instance events in your Worker or via the API with .subscribe() | ||
| description: Workflow instances now support a .subscribe() method, which let Workers and applications consume events as instances run. | ||
| products: | ||
| - workflows | ||
| - workers | ||
| date: 2026-09-11 12:00:00 UTC | ||
| --- | ||
|
|
||
| import { TypeScriptExample } from "~/components"; | ||
|
|
||
| You can now stream Workflow instance events via `WorkflowInstance.subscribe()` and the `GET /subscribe` API endpoint. Workers and HTTP clients can react to [workflow](/workflows/build/events-and-parameters/) and [step](/workflows/build/step-context/#workflowstepcontext) events, including attempts, sleeps, waits, and rollbacks, without polling for instance status. | ||
|
|
||
| A subscription first streams the entire event history of the Workflow instance. After streaming past events, the subscription waits for new events as the instance runs. You can use `filter` to receive only specific event types or `cursor` to start a subscription at a specific event. | ||
|
|
||
| Use `.subscribe()` to update Workflow status in user-facing dashboards, send notifications when steps complete, or trigger follow-up work for specific events. | ||
|
|
||
| <TypeScriptExample> | ||
|
|
||
| ```ts | ||
| const instance = await env.MY_WORKFLOW.get("report-123"); | ||
|
|
||
| using subscription = await instance.subscribe(); | ||
|
|
||
| while (true) { | ||
| const { value, done } = await subscription.next(); | ||
| if (done) { | ||
| break; | ||
| } | ||
|
|
||
| console.log(value.type, value); | ||
| } | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| For event types, available fields, and subscription options, refer to [Subscribe to events](/workflows/build/subscribe-to-instance-events/). |
261 changes: 261 additions & 0 deletions
261
src/content/docs/workflows/build/subscribe-to-instance-events.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| --- | ||
| title: Subscribe to instance events | ||
| description: Use Cloudflare Workflows subscribe method to receive historical and live instance events without polling status. | ||
| pcx_content_type: concept | ||
| sidebar: | ||
| order: 7 | ||
| products: | ||
| - workflows | ||
| --- | ||
|
|
||
| import { TypeScriptExample } from "~/components"; | ||
|
|
||
| Use `WorkflowInstance.subscribe()` to receive events without polling [`status()`](/workflows/build/workers-api/#status). A subscription first delivers events recorded before you subscribed. After delivering these retained events, the subscription waits for new events as the instance runs. | ||
|
|
||
| You can subscribe immediately after creating an instance. To subscribe later, retrieve the instance with [`get()`](/workflows/build/workers-api/#get). Subscriptions remain available during the [instance retention period](/workflows/reference/limits/). | ||
|
|
||
| ## Subscribe to all events | ||
|
|
||
| <TypeScriptExample> | ||
|
|
||
| ```ts | ||
| interface Env { | ||
| MY_WORKFLOW: Workflow; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(_request: Request, env: Env) { | ||
| const instance = await env.MY_WORKFLOW.create({ | ||
| params: { reportId: "report-123" }, | ||
| }); | ||
|
|
||
| using subscription = await instance.subscribe(); | ||
|
|
||
| while (true) { | ||
| const result = await subscription.next(); | ||
| if (result.done) { | ||
| break; | ||
| } | ||
|
|
||
| console.log(result.value.type, result.value); | ||
| } | ||
|
|
||
| return Response.json({ instanceId: instance.id }); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| The subscription ends when the instance emits `workflow_completed`, `workflow_errored`, or `workflow_terminated`. After a terminal event, each later `next()` call returns `done: true`. | ||
|
|
||
| ## Filter events | ||
|
|
||
| Set `filter` to limit `next()` results to specific event types. | ||
|
|
||
| <TypeScriptExample> | ||
|
|
||
| ```ts | ||
| const instance = await env.MY_WORKFLOW.get("report-123"); | ||
|
|
||
| using subscription = await instance.subscribe({ | ||
| filter: ["workflow_completed", "workflow_errored", "workflow_terminated"], | ||
| }); | ||
|
|
||
| const result = await subscription.next(); | ||
| if (result.done) { | ||
| throw new Error("The instance ended without a matching event."); | ||
| } | ||
|
|
||
| switch (result.value.type) { | ||
| case "workflow_completed": | ||
| console.log("Workflow output:", result.value.output); | ||
| break; | ||
| case "workflow_errored": | ||
| console.error("Workflow errored:", result.value.error); | ||
| break; | ||
| case "workflow_terminated": | ||
| console.log("Workflow terminated."); | ||
| break; | ||
| } | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| A subscription ends even when its filter excludes a terminal event. In that case, `next()` returns `done: true` without the event. | ||
|
|
||
| ## Resume from a cursor | ||
|
|
||
| Each event includes an `eventId`. To resume after a remote procedure call (RPC) fails, store the last processed event ID. Then pass that ID as `cursor`: | ||
|
|
||
| <TypeScriptExample> | ||
|
|
||
| ```ts | ||
| using subscription = await instance.subscribe({ | ||
| cursor: lastProcessedEventId, | ||
| filter: ["step_completed", "workflow_completed", "workflow_errored"], | ||
| }); | ||
|
|
||
| while (true) { | ||
| const result = await subscription.next(); | ||
| if (result.done) { | ||
| break; | ||
| } | ||
|
|
||
| await processEvent(result.value); | ||
| await saveLastProcessedEventId(result.value.eventId); | ||
| } | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| The cursor identifies the last processed event. The subscription starts with the first event whose `eventId` is greater than the cursor. | ||
|
|
||
| ## Sensitive outputs | ||
|
|
||
| For steps marked as sensitive, the `step_completed` event sets `output` to `"[REDACTED]"`. | ||
|
|
||
| ## Dispose of a subscription | ||
|
|
||
| A subscription holds a Workers RPC resource. Disposing the subscription stops event delivery, clears its state, and releases its resources. | ||
|
|
||
| Declare the subscription with `using` for automatic disposal when the scope exits, or call `subscription[Symbol.dispose]()` in a `finally` block. For more information, refer to [RPC lifecycle](/workers/runtime-apis/rpc/lifecycle/). | ||
|
|
||
| ## Event fields | ||
|
|
||
| The public type definition shows the fields available on each event: | ||
|
|
||
| <TypeScriptExample> | ||
|
|
||
| ```ts | ||
| type WorkflowInstanceEvent = { | ||
| instanceId: string; | ||
| eventId: number; | ||
| timestamp: number; | ||
| } & ( | ||
| | { type: "workflow_queued" } | ||
| | { type: "workflow_started"; params?: unknown } | ||
| | { type: "workflow_running" } | ||
| | { type: "workflow_paused" } | ||
| | { type: "workflow_waiting_for_pause" } | ||
| | { type: "workflow_waiting" } | ||
| | { type: "workflow_completed"; output?: unknown } | ||
| | { type: "workflow_errored"; error: { name: string; message: string } } | ||
| | { type: "workflow_terminated" } | ||
| | { | ||
| type: "step_started"; | ||
| stepName: string; | ||
| config?: { | ||
| retries: { | ||
| limit: number; | ||
| delay: WorkflowSleepDuration | "[dynamic]"; | ||
| backoff?: "constant" | "linear" | "exponential"; | ||
| }; | ||
| timeout: WorkflowSleepDuration; | ||
| sensitive?: "output"; | ||
| }; | ||
| } | ||
| | { type: "step_completed"; stepName: string; output?: unknown } | ||
| | { type: "step_errored"; stepName: string } | ||
| | { type: "attempt_started"; stepName: string; attempt: number } | ||
| | { type: "attempt_completed"; stepName: string; attempt: number } | ||
| | { | ||
| type: "attempt_errored"; | ||
| stepName: string; | ||
| attempt: number; | ||
| retryDelayMs?: number; | ||
| error: { name: string; message: string }; | ||
| } | ||
| | { type: "sleep_started"; stepName: string; durationMs: number } | ||
| | { type: "sleep_completed"; stepName: string } | ||
| | { type: "wait_started"; stepName: string; eventType: string } | ||
| | { type: "wait_completed"; stepName: string } | ||
| | { type: "wait_timed_out"; stepName: string } | ||
| | { type: "rollback_started" } | ||
| | { | ||
| type: "rollback_step_started"; | ||
| stepName: string; | ||
| config?: { | ||
| retries: { | ||
| limit: number; | ||
| delay: WorkflowSleepDuration | "[dynamic]"; | ||
| backoff?: "constant" | "linear" | "exponential"; | ||
| }; | ||
| timeout: WorkflowSleepDuration; | ||
| sensitive?: "output"; | ||
| }; | ||
| } | ||
| | { type: "rollback_step_completed"; stepName: string } | ||
| | { | ||
| type: "rollback_step_errored"; | ||
| stepName: string; | ||
| error: { name: string; message: string }; | ||
| } | ||
| | { type: "rollback_attempt_started"; stepName: string; attempt: number } | ||
| | { type: "rollback_attempt_completed"; stepName: string; attempt: number } | ||
| | { | ||
| type: "rollback_attempt_errored"; | ||
| stepName: string; | ||
| attempt: number; | ||
| retryDelayMs?: number; | ||
| error: { name: string; message: string }; | ||
| } | ||
| | { type: "rollback_completed" } | ||
| | { type: "rollback_errored" } | ||
| ); | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| The following sections describe when each event is emitted. | ||
|
|
||
| ### Workflow lifecycle events | ||
|
|
||
| | Event type | Emitted when | | ||
| | ---------------------------- | ------------------------------------------------ | | ||
| | `workflow_queued` | The instance enters the execution queue | | ||
| | `workflow_started` | The instance starts | | ||
| | `workflow_running` | The instance starts or resumes execution | | ||
| | `workflow_paused` | The instance pauses | | ||
| | `workflow_waiting_for_pause` | The instance waits for current work before pause | | ||
| | `workflow_waiting` | The instance enters waiting state | | ||
| | `workflow_completed` | The instance completes successfully | | ||
| | `workflow_errored` | The instance ends with an error | | ||
| | `workflow_terminated` | The instance is terminated | | ||
|
|
||
| ### Step and attempt events | ||
|
|
||
| | Event type | Emitted when | | ||
| | ------------------- | ---------------------------- | | ||
| | `step_started` | A `step.do()` call starts | | ||
| | `step_completed` | A `step.do()` call completes | | ||
| | `step_errored` | A `step.do()` call errors | | ||
| | `attempt_started` | A step attempt starts | | ||
| | `attempt_completed` | A step attempt completes | | ||
| | `attempt_errored` | A step attempt errors | | ||
|
|
||
| ### Sleep and wait events | ||
|
|
||
| | Event type | Emitted when | | ||
| | ----------------- | --------------------------------------------------- | | ||
| | `sleep_started` | A `step.sleep()` or `step.sleepUntil()` call starts | | ||
| | `sleep_completed` | A sleep finishes | | ||
| | `wait_started` | A `step.waitForEvent()` call starts | | ||
| | `wait_completed` | A matching event reaches `step.waitForEvent()` | | ||
| | `wait_timed_out` | A `step.waitForEvent()` call times out | | ||
|
|
||
| ### Rollback events | ||
|
|
||
| | Event type | Emitted when | | ||
| | ---------------------------- | --------------------------------------- | | ||
| | `rollback_started` | The Workflow starts a rollback | | ||
| | `rollback_step_started` | A rollback handler starts | | ||
| | `rollback_step_completed` | A rollback handler completes | | ||
| | `rollback_step_errored` | A rollback handler errors | | ||
| | `rollback_attempt_started` | A rollback attempt starts | | ||
| | `rollback_attempt_completed` | A rollback attempt completes | | ||
| | `rollback_attempt_errored` | A rollback attempt errors | | ||
| | `rollback_completed` | All required rollback handlers complete | | ||
| | `rollback_errored` | The rollback operation errors | | ||
|
|
||
| For method signatures and option types, refer to [`WorkflowInstance.subscribe()`](/workflows/build/workers-api/#subscribe). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.