NATS icon

NATS

Send messages to NATS subjects

Overview

This node sends messages to NATS subjects, a lightweight, high-performance messaging system used for cloud-native applications and microservices. It is useful for publishing event notifications, commands, or data streams to specific subjects (topics) in a NATS server.

Common scenarios include:

  • Sending real-time updates from IoT sensors.
  • Publishing order events in e-commerce systems.
  • Broadcasting notifications or alerts across distributed services.
  • Implementing request-reply patterns with optional reply subjects.

For example, you can publish a message with subject orders.new containing new order details, which other services subscribed to that subject can consume.

Properties

Name Meaning
Subject The subject name for message routing. No spaces allowed. Use dot notation for hierarchy, e.g., orders.new, sensors.temperature.room1.
Data The content of the message to publish. Can be any string, often JSON serialized data. Defaults to the current input item’s JSON.
Headers Optional key-value pairs added as headers to the message. Useful for routing metadata or additional context. Multiple headers can be added.
Options Additional options for the message:
- Reply To: Subject name to receive replies, enabling request-reply communication patterns.

Output

The node outputs an array with one object per input item, each containing a json field structured as follows:

  • success: Boolean indicating if the message was published successfully.
  • subject: The subject name to which the message was sent.
  • timestamp: ISO 8601 timestamp of when the message was published.

If an error occurs and the node is configured to continue on failure, the output for that item will contain:

  • error: Error message describing what went wrong.
  • subject: The subject attempted.

No binary data is produced by this node.

Dependencies

  • Requires a connection to a NATS server.
  • Needs an API authentication credential configured in n8n to connect securely to the NATS server.
  • Uses internal utility modules for managing connections, validating subjects, creating headers, and logging.

Troubleshooting

  • Invalid Subject Format: The subject must not contain spaces and should follow NATS subject naming conventions (dots for hierarchy). Errors related to invalid subjects will be thrown during validation.
  • Connection Failures: If the node cannot connect to the NATS server, ensure the credentials and network access are correctly configured.
  • Publish Failures: Errors during publishing may occur due to server issues or malformed messages. Check the error message for details.
  • Continue On Fail: When enabled, errors for individual items do not stop execution; instead, error details are returned in the output for those items.

Links and References

Discussion