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-natsUsage
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
| Option | Type | Description |
|---|---|---|
servers | string[] | NATS server URLs |
subject | string | Subject to subscribe to |
queue | string | Optional queue group name for load balancing |
deliverPolicy | "all" | "last" | "new" | Optional message delivery policy |
credentials | object | Optional authentication credentials |
credentials.user | string | Username |
credentials.pass | string | Password |
credentials.token | string | Authentication token |
Event Metadata
The event metadata contains NATS-specific information:
{
subject: string;
reply?: string;
headers: Record<string, string>;
sid: number;
}