NATS JetStream icon

NATS JetStream

Manage NATS JetStream streams, consumers, and publish messages

Actions11

Overview

This node integrates with NATS JetStream, a high-performance messaging system, to publish messages to specified subjects within the JetStream environment. The "Publish" operation under the "Message" resource allows users to send messages with custom content and headers, supporting advanced message routing and metadata tagging.

Typical use cases include:

  • Sending event notifications in distributed systems (e.g., events.orders.new).
  • Publishing sensor data streams with hierarchical subject naming (e.g., sensors.temperature.room1).
  • Adding custom headers for message filtering or routing downstream.
  • Using message options like deduplication IDs or optimistic concurrency controls to ensure message integrity.

Example: Publishing an order creation event with subject events.orders.new, JSON payload describing the order, and headers indicating content type or priority.

Properties

Name Meaning
Subject The subject name used for message routing. It must not contain spaces and supports dot notation for hierarchy (e.g., events.orders.new, sensors.temperature.room1).
Data The content of the message to be published. Typically a JSON string or any stringified data representing the message payload. Defaults to the current input JSON item.
Headers Optional collection of key-value pairs to add as headers to the message. Headers can carry metadata or be used for routing decisions by consumers. Each header has a name and a value (e.g., Content-Type: application/json).
Options Additional optional parameters for publishing:
- Message ID: A unique identifier for the message to enable deduplication.
- Expected Last Message ID: For optimistic concurrency control.
- 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. Each output JSON contains the result of the publish operation, which may include confirmation details such as message acknowledgment or metadata returned by the JetStream server.

If an error occurs during publishing and the node is configured to continue on failure, the output JSON will contain an error field describing the issue along with the resource and operation names.

The node does not output binary data.

Dependencies

  • Requires a valid connection to a NATS JetStream server.
  • Needs an API authentication credential (API key or token) configured in n8n to establish the connection.
  • Uses bundled NATS client libraries internally; no additional external dependencies are required beyond the configured credentials and network access to the NATS server.

Troubleshooting

  • Invalid Subject Format: The subject must not contain spaces and should follow the dot-separated hierarchy convention. Errors related to invalid subjects indicate this rule was violated.
  • Unknown Operation or Resource: If the operation or resource name is incorrect or unsupported, the node throws an error specifying the unknown operation/resource.
  • Connection Failures: Issues connecting to the NATS server usually stem from incorrect credentials, network issues, or server unavailability.
  • Message Deduplication Conflicts: When using message IDs for deduplication, conflicts may arise if duplicate IDs are sent within the deduplication window.
  • Optimistic Concurrency Errors: Using expected last message ID or sequence requires correct values; mismatches cause errors indicating concurrent modification.
  • To resolve errors, verify all input parameters, ensure proper credential configuration, and confirm the NATS JetStream server is reachable and correctly set up.

Links and References

Discussion