StepflowStepflow

NATS Trigger

Trigger workflows from NATS messages

NATS Trigger

The NATS trigger allows you to initiate workflows by subscribing to NATS subjects. It supports queue groups for load balancing and various delivery policies.

Installation

pnpm add @stepflowjs/trigger-nats

Usage

import { NatsTrigger } from "@stepflowjs/trigger-nats";

const trigger = new NatsTrigger({
  servers: ["nats://localhost:4222"],
  subject: "orders.created",
  queue: "order-processors",
  deliverPolicy: "new",
});

await trigger.start(async (event) => {
  console.log("Received NATS message:", event.data);
  await stepflow.trigger("process-order", event.data);
});

Queue Groups

By specifying a queue name in the configuration, you can enable load balancing across multiple instances of your application. NATS will ensure that each message is delivered to only one instance in the queue group.

Configuration

OptionTypeDescription
serversstring[]NATS server URLs
subjectstringSubject to subscribe to
queuestringOptional queue group name for load balancing
deliverPolicy"all" | "last" | "new"Optional message delivery policy
credentialsobjectOptional authentication credentials
credentials.userstringUsername
credentials.passstringPassword
credentials.tokenstringAuthentication token

Event Metadata

The event metadata contains NATS-specific information:

{
  subject: string;
  reply?: string;
  headers: Record<string, string>;
  sid: number;
}

On this page