Overview
This node sends messages to NATS subjects, a lightweight, high-performance messaging system used for cloud-native applications and microservices. It is designed to publish messages on specified subjects (topics) with optional headers and reply-to options.
Common scenarios include:
- Event-driven architectures where services communicate asynchronously.
- IoT data streaming by publishing sensor data to hierarchical subjects.
- Request-reply messaging patterns using the reply-to feature.
For example, you can publish an order update message to the subject orders.new with custom headers for routing or metadata, or send temperature readings from different rooms using subjects like sensors.temperature.room1.
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. |
| Headers | Optional key-value pairs added as headers to the message. Useful for routing and metadata. |
| Options | Additional options for the message: - Reply To: Subject name to receive replies, enabling request-reply messaging pattern. |
Output
The node outputs an array with one item per input message. Each output item contains a json object with:
success: Boolean indicating if the message was published successfully.subject: The subject name the message was published to.timestamp: ISO string timestamp of when the message was sent.
If an error occurs and the node is configured to continue on failure, the output will contain an error field with the error message and the attempted subject.
No binary data is produced by this node.
Dependencies
- Requires a connection to a NATS server, authenticated via an API key credential.
- Uses utility modules for managing NATS connections, validating subjects, creating headers, and logging.
- The node expects proper configuration of the NATS server credentials in n8n.
Troubleshooting
- Invalid Subject Error: If the subject contains spaces or invalid characters, the node will throw an error. Ensure the subject uses only valid characters and dot notation for hierarchy.
- Connection Failures: Errors connecting to the NATS server usually indicate incorrect credentials or network issues. Verify the API key and server accessibility.
- Publish Failures: If publishing fails, check that the subject exists and the server allows publishing to it.
- Continue On Fail: When enabled, errors for individual messages do not stop execution; instead, error details are returned in the output for those messages.