NATS JetStream icon

NATS JetStream

Manage NATS JetStream streams, consumers, and publish messages

Actions11

Overview

This node integrates with NATS JetStream, a messaging system for streaming data. Specifically, the Publish operation under the Message resource allows users to send messages to a specified subject within the JetStream environment.

Typical use cases include:

  • Sending event notifications (e.g., events.orders.new) to other services or microservices listening on those subjects.
  • Publishing sensor data updates in real-time (e.g., sensors.temperature.room1).
  • Distributing commands or signals across distributed systems using hierarchical subject naming.

This operation is beneficial when you want to reliably publish messages with optional headers and advanced options like message deduplication or optimistic concurrency control.

Properties

Name Meaning
Subject The subject name used for routing the message. It must not contain spaces and can use dots (.) to define a hierarchy (e.g., events.orders.new).
Data The content of the message to be published. This is typically a JSON string or any stringified data representing the payload.
Headers Optional key-value pairs added as headers to the message. Headers can carry metadata or be used for routing purposes. Multiple headers can be added. Each header has a "Name" and a "Value".
Options Additional optional settings for publishing:
- Message ID: An optional identifier for the message to enable deduplication.
- Expected Last Message ID: For optimistic concurrency, specifying the last known message ID.
- Expected Last Sequence: Expected sequence number of the last message.
- Expected Stream: Validates the stream name before publishing.

Output

The node outputs an array of JSON objects corresponding to each input item processed. For the Publish operation, the output JSON contains the result returned by the JetStream publish call, which typically includes metadata about the published message such as acknowledgment details.

No binary data output is produced by this operation.

Example output JSON structure (simplified):

{
  "stream": "example-stream",
  "seq": 123,
  "duplicate": false
}

Where:

  • stream is the name of the stream where the message was published.
  • seq is the sequence number assigned to the message.
  • duplicate indicates if the message was considered a duplicate based on the provided message ID.

Dependencies

  • Requires a valid connection to a NATS JetStream server.
  • Needs an API authentication credential configured in n8n to connect securely to the NATS server.
  • Uses bundled NATS client libraries internally; no additional external dependencies are required beyond the configured credentials.

Troubleshooting

  • Invalid Subject Error: If the subject contains spaces or invalid characters, the node will throw an error. Ensure the subject uses only allowed characters and dot notation for hierarchy.
  • Unknown Operation or Resource: Errors occur if unsupported operations or resources are selected. Verify that "Message" resource and "Publish" operation are chosen.
  • Connection Failures: If the node cannot connect to the NATS server, check network connectivity, server availability, and correctness of the API credentials.
  • Duplicate Message Handling: When using message IDs for deduplication, ensure IDs are unique per message unless intentional duplicates are expected.
  • Optimistic Concurrency Errors: Using expected last message ID or sequence requires accurate values; mismatches may cause publish failures.

If errors occur, enabling "Continue On Fail" allows processing subsequent items without stopping the workflow.

Links and References


This summary is based solely on static analysis of the provided source code and property definitions.

Discussion