Skip to main content
Upstash Realtime lets you emit events from your workflow and subscribe to them in real-time on your frontend.

How It Works

Upstash Realtime is powered by Upstash Redis and provides a clean, 100% type-safe API for publishing and subscribing to events:
  • Your frontend can subscribe to events
  • When you emit an event, it’s instantly delivered to live subscribers on the frontend
  • You can also replay events that happened in the past
This guide shows you how to integrate Upstash Workflow with Upstash Realtime to display real-time progress updates in your frontend.

Setup

1. Install Packages

2. Configure Upstash Realtime

Create a Realtime instance in lib/realtime.ts:
lib/realtime.ts

3. Create a Realtime Middleware

Create a custom middleware that will emit events to Realtime at lib/middleware.ts:
lib/middleware.ts
Key points:
  • The afterExecution callback is triggered after each workflow step completes
  • The runCompleted callback is triggered when the entire workflow finishes
  • We use context.workflowRunId to create a unique channel for each workflow run
  • Events are automatically emitted without needing to manually call emit inside your workflow steps

4. Create a Realtime Endpoint

Create an API route at app/api/realtime/route.ts to handle Realtime connections:
app/api/realtime/route.ts
This endpoint enables Server-Sent Events (SSE) connections for real-time updates.

5. Add the Realtime Provider

Wrap your application in the RealtimeProvider by updating your root layout at app/layout.tsx:
app/layout.tsx

6. Create a Typed Client Hook

Create a typed useRealtime hook at lib/realtime-client.ts:
lib/realtime-client.ts

Building the Workflow

1. Create the Workflow Endpoint

Create your workflow at app/api/workflow/route.ts:
app/api/workflow/route.ts
Key points:
  • Import the realtimeMiddleware from @/lib/middleware
  • Pass the middleware in the middlewares array to the serve function
  • The middleware automatically emits events after each step and when the workflow completes

2. Create a Trigger Endpoint

Create an endpoint to trigger workflows at app/api/trigger/route.ts:
app/api/trigger/route.ts

Building the Frontend

1. Create a Custom Hook

Create a React hook to manage the Realtime subscription at hooks/useWorkflow.ts:
Key features:
  • Subscribe to multiple events using the events array: ["workflow.stepFinish", "workflow.runFinish"]
  • The hook manages both triggering the workflow and subscribing to updates
  • Type-safe event handling with TypeScript

2. Use the Hook in Your Component

How It All Works Together

  1. User triggers workflow: The frontend calls /api/trigger, which returns a workflowRunId
  2. Frontend subscribes: Using the workflowRunId, the frontend subscribes to the Realtime channel
  3. Workflow executes: The workflow runs as a background job, emitting events at each step
  4. Real-time updates: As the workflow emits events, they’re instantly delivered to the frontend via Server-Sent Events

Full Example

For a complete working example with all steps, error handling, and UI components, check out the Upstash Realtime example on GitHub.

Next Steps