Keel by ChimpLabs
Jump to page or section Esc
Primitives

Signal

Typed external signals that wake suspended workflows in real time via PostgreSQL LISTEN/NOTIFY, SurrealDB LIVE SELECT, or in-memory push via @chimp-labs/keel-workflow.

Signal (@chimp-labs/keel-workflow) delivers a schema-validated external message to a running Workflow identified by runId. Typical messages include human approvals, third-party callbacks, and cancellation requests.

Use Signal when an external actor must resume or send data to one workflow run. For broadcast domain events consumed by multiple subscribers, see Topic.

pnpm add @chimp-labs/keel @chimp-labs/keel-workflow effect

Declaring a signal

Plain TypeScript SDK (@chimp-labs/keel-workflow + @chimp-labs/keel/sdk)

import { createKeel, k } from "@chimp-labs/keel/sdk"
import { signal } from "@chimp-labs/keel-workflow"

export const ApprovalSignal = signal(
  "order.approved",
  k.object({
    approvedBy: k.string(),
    notes: k.optional(k.string()),
  })
)

const keel = await createKeel({
  backend: "memory",
  resources: { approval: ApprovalSignal },
})

Effect v4 (@chimp-labs/keel-workflow)

import { Effect, Schema } from "effect"
import { Signal, sendSignal, WorkflowStoreMemory } from "@chimp-labs/keel-workflow"

export class ApprovalSignal extends Signal("order.approved", {
  payload: Schema.Struct({
    approvedBy: Schema.String,
    notes: Schema.optional(Schema.String),
  }),
}) {}

await Effect.runPromise(
  sendSignal("order-ord_42", ApprovalSignal, {
    approvedBy: "[email protected]",
  }).pipe(Effect.provide(WorkflowStoreMemory))
)

Attaching to a service

export const ApprovalsService = k.service("approvals", {
  resources: [FulfillmentWorkflow, ApprovalSignal],
  operations: { /* ... */ },
})

Operations reference

Waiting for a signal inside a workflow

Inside a workflow handler, call ctx.waitForSignal(stepName, Signal, { timeout? }). Keel persists signals in the workflow journal. If a signal arrives before the workflow reaches waitForSignal, Keel buffers the signal and delivers it when the step runs.

const approval = await ctx.waitForSignal(
  "wait-manager-approval",
  ApprovalSignal,
  { timeout: "48 hours" }
)
// approval is typed as { approvedBy: string; notes?: string }

Sending a signal from an HTTP handler or CLI

await keel.approval.send("order-ord_42", {
  approvedBy: "[email protected]",
  notes: "Verified high-value customer",
})

SDK and Effect API mapping

SDK method (keel.<signal>.* / ctx.*) Effect function (@chimp-labs/keel-workflow) Description
send(runId, payload) sendSignal(runId, signal, payload) Validate and deliver a typed signal to runId.
ctx.waitForSignal(name, signal, opts?) ctx.waitForSignal(name, signal, opts?) Suspend the workflow until the signal arrives or timeout elapses.

Errors and failure modes

Error tag Category HTTP status Retryable When it occurs
WorkflowTimeoutError timeout 504 Yes No matching signal arrived before timeout elapsed.
WorkflowStoreError internal 500 Yes Signal persistence or delivery failed.
SchemaError invalid 400 No The signal payload failed schema validation.

Backends and infrastructure

  • Memory (WorkflowStoreMemory): Wakes suspended workflows in process and buffers signals that arrive early.
  • PostgreSQL 17 (@chimp-labs/keel-workflow/postgres): WorkflowStorePostgresLive stores durable signal rows alongside workflow journals and wakes suspended workflows across replicas through LISTEN/NOTIFY without polling.
  • SQLite (WorkflowStoreFromTable): Stores signals in a durable keel_workflow_signals table alongside workflow step journals.
  • SurrealDB 4 (@chimp-labs/keel-workflow/surreal): WorkflowStoreSurrealLive wakes suspended workflows via LIVE SELECT without polling.

Testing in memory

import { createKeel, k } from "@chimp-labs/keel/sdk"
import { workflow, signal } from "@chimp-labs/keel-workflow"

const FulfillmentWorkflow = workflow("order.fulfillment", {
  input: k.object({ orderId: k.string(), sku: k.string(), qty: k.int(), totalCents: k.int() }),
  output: k.object({ reservationId: k.string(), trackingCode: k.string() }),
})

const ApprovalSignal = signal("order.approved", k.object({ approvedBy: k.string() }))

await using keel = await createKeel({
  backend: "memory",
  resources: {
    fulfillment: FulfillmentWorkflow,
    approval: ApprovalSignal,
  },
})

const runPromise = keel.fulfillment.run(
  { runId: "wf-1", input: { orderId: "1", sku: "A", qty: 1, totalCents: 1000 } },
  async (_inp, ctx) => {
    const sig = await ctx.waitForSignal("approve", ApprovalSignal, {
      timeout: "5 seconds",
    })
    return { reservationId: "res-1", trackingCode: `APPROVED-${sig.approvedBy}` }
  }
)

await keel.approval.send("wf-1", { approvedBy: "alice" })
const result = await runPromise
// result.trackingCode === "APPROVED-alice"