Skip to main content
Some workflows require human approval or input before proceeding. When combined with Upstash Realtime, you can create interactive workflows that pause for user input and provide real-time feedback to your frontend during the entire process. This guide shows you how to implement a human-in-the-loop workflow pattern with real-time updates using Upstash Workflow and Upstash Realtime.

How It Works

In a human-in-the-loop workflow:
  1. The workflow executes initial steps and emits progress events
  2. The workflow pauses at a specific point using context.waitForEvent()
  3. A “waiting for input” event is emitted to notify the frontend
  4. The user makes a decision in the frontend (approve/reject)
  5. The frontend calls an API to notify the workflow using client.notify()
  6. The workflow resumes with the user’s decision
  7. An “input resolved” event is emitted so the frontend can update its UI
  8. The workflow continues and completes based on the decision

Prerequisites

  • An Upstash account with:
    • A QStash project for workflows
    • A Redis database for Realtime
  • Next.js application set up
  • Completed the basic real-time workflow setup

Event Types

For human-in-the-loop workflows, extend your schema in lib/realtime.ts with these additional event types:
The new event types are:
  • waitingForInput: Emitted when the workflow pauses and needs user input
  • inputResolved: Emitted when the user provides input, so the frontend knows to clear the waiting state

Create the Realtime Middleware

Create a custom middleware that will emit events to Realtime at lib/middleware.ts:
lib/middleware.ts
Key points:
  • The middleware handles all realtime event emissions automatically
  • beforeExecution: Detects wait-for-event steps by checking the stepName and emits workflow.waitingForInput
  • afterExecution: Emits workflow.inputResolved for wait steps and workflow.stepFinish for all steps
  • runCompleted: Emits workflow.runFinish when the workflow finishes
  • All emission logic is centralized in the middleware, keeping workflow code clean

Building the Workflow

1. Create the Workflow Endpoint

Create your workflow at app/api/workflow/human-in-loop/route.ts:
app/api/workflow/human-in-loop/route.ts
Key patterns:
  1. Middleware for all events: The realtimeMiddleware automatically handles all realtime event emissions by detecting wait-for-event steps through the stepName
  2. Step name detection: The middleware checks if stepName === "wait-for-approval" to know when to emit waitingForInput and inputResolved events
  3. Unique event IDs: Use a unique eventId (like approval-${workflowRunId}) to identify which approval request this is
  4. Timeout handling: Always handle the timeout case when waiting for events

2. Create the Notify Endpoint

Create an endpoint at app/api/notify/route.ts to handle user input:
Preventing Race Conditions: If you need to trigger a workflow and immediately send it an event, you can use the workflowRunId parameter to enable lookback:
Learn more in the notify documentation.

Building the Frontend

1. Extend the Custom Hook

Extend your hook from the basic example to handle waiting states:
Key additions:
  • waitingState: Tracks when the workflow is waiting for input
  • continueWorkflow: Function to submit user decisions back to the workflow
  • Multiple events subscription: Uses events array to subscribe to multiple event types
  • Input resolved handling: Clears the waiting state when the workflow receives the user’s input

2. Use the Hook with Approval UI

How the Pattern Works

Timeline of Events

  1. Initial processing: stepFinish event → Frontend shows completed step
  2. Waiting for approval: waitingForInput event → Frontend shows approval buttons
  3. User clicks approve/reject: Frontend calls /api/notify
  4. Workflow resumes: inputResolved event → Frontend hides approval buttons
  5. Processing continues: More stepFinish events as workflow continues
  6. Workflow completes: runFinish event → Frontend shows “Workflow Finished!”

Benefits

  • Real-time feedback: Users see exactly when their approval is needed
  • No polling: Instant updates via Server-Sent Events
  • Timeout handling: Workflows don’t hang indefinitely waiting for input

Full Example

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

Next Steps