MQTT Out

The MQTT Out node connects to an MQTT broker and publishes messages to one or more topics.
It is typically used to send sensor data, commands, or event notifications from Node-RED to external systems or devices that subscribe to MQTT topics.

How it works

When a message arrives at the node input, it publishes the content of msg.payload to the specified topic on the broker.
If no topic is set in the node, it must be provided in msg.topic.

If msg.payload is not set, no message will be sent.
To send an empty message, set msg.payload to an empty string ("").

Configuration

The MQTT Out node requires a connection to an MQTT broker.
This can be configured by clicking the pencil icon next to the Server field.
Multiple MQTT nodes (in or out) can share the same broker configuration.

Server

Defines the broker connection details:

  • Broker address (for example, test.mosquitto.org)
  • Port (default 1883 for non-TLS, 8883 for TLS)
  • Optional username and password
  • MQTT version (v3.1, v3.1.1, or v5)
  • Enable TLS for encrypted connections
  • Client ID and session options

Topic

Specifies the MQTT topic to publish to.
You can enter a fixed topic or leave it blank to use msg.topic dynamically.

QoS

Defines the message delivery quality level:

  • 0 – fire and forget
  • 1 – at least once
  • 2 – once and once only (default)

The QoS can be overridden at runtime using msg.qos.

Retain

If set to true, the broker will retain the last message sent to the topic and deliver it to new subscribers immediately.
You can override this in the flow using msg.retain.

To clear a retained topic, send a blank message (msg.payload = "") with msg.retain = true.

Input properties

The MQTT Out node accepts the following properties in incoming messages:

  • msg.payload (string | buffer) – the message payload to publish.
    If it contains an object, it is automatically converted to a JSON string.
    If it contains a buffer, it is sent as-is.
  • msg.topic (string) – the topic to publish to. Required if not defined in the node.
  • msg.qos (number) – overrides the configured QoS (0, 1, or 2).
  • msg.retain (boolean) – overrides the retain flag.

MQTT v5 properties

If the broker and node are using MQTT version 5, the following properties can also be set:

  • msg.responseTopic (string) – the MQTT response topic for the message
  • msg.correlationData (buffer) – correlation data for the message
  • msg.contentType (string) – the content type of the payload
  • msg.userProperties (object) – any user-defined properties
  • msg.messageExpiryInterval (number) – expiry time, in seconds
  • msg.topicAlias (number) – topic alias to use

Dynamic control

The MQTT Out node can also respond to special control messages to manage the connection dynamically.
If one of these control messages is received, the node will perform the action but will not publish the payload.

msg.action

Defines the action to perform. Supported actions:

  • connect – establish a connection to the broker
  • disconnect – close the current connection

msg.broker

For the connect action, this property can override broker configuration options dynamically, including:

  • broker
  • port
  • url (overrides both broker and port)
  • username
  • password

If the node is already connected and new settings are provided, it will log an error unless the property force is set to true.
In that case, it will disconnect, apply the new configuration, and reconnect.

Example: Simple Publish

This example shows how to publish a timestamp to an MQTT topic on the public Mosquitto test broker.

Notes

  • The node automatically converts objects to JSON strings when publishing.
  • For large payloads or binary data, use buffers to avoid unnecessary conversion.
  • Multiple MQTT Out nodes can share the same broker connection.
  • MQTT v5 users can take advantage of additional properties for richer message metadata.

Node help

This is the MQTT Out node's built-in help, mirrored from the Node-RED project. It is the same text the editor shows in its Info sidebar.

Connects to a MQTT broker and publishes messages.

Inputs

payload string | buffer
the payload to publish. If this property is not set, no message will be sent. To send a blank message, set this property to an empty String.
topic string
the MQTT topic to publish to.
qos number
0, fire and forget - 1, at least once - 2, once and once only. Default 0.
retain boolean
set to true to retain the message on the broker. Default false.
responseTopic string
MQTTv5: the MQTT response topic for the message
correlationData Buffer
MQTTv5: the correlation data for the message
contentType string
MQTTv5: the content-type of the payload
userProperties object
MQTTv5: any user properties of the message
messageExpiryInterval number
MQTTv5: the expiry time, in seconds, of the message
topicAlias number
MQTTv5: the MQTT topic alias to use

Details

msg.payload is used as the payload of the published message. If it contains an Object it will be converted to a JSON string before being sent. If it contains a binary Buffer the message will be published as-is.

The topic used can be configured in the node or, if left blank, can be set by msg.topic.

Likewise the QoS and retain values can be configured in the node or, if left blank, set by msg.qos and msg.retain respectively. To clear a previously retained topic from the broker, send a blank message to that topic with the retain flag set.

This node requires a connection to a MQTT broker to be configured. This is configured by clicking the pencil icon.

Several MQTT nodes (in or out) can share the same broker connection if required.

Dynamic Control

The connection shared by the node can be controlled dynamically. If the node receives one of the following control messages, it will not publish the message payload as well.

Inputs

action string
the name of the action the node should perform. Available actions are: "connect", and "disconnect".
broker broker
For the "connect" action, this property can override any of the individual broker configuration settings, including:
  • broker
  • port
  • url - overrides broker/port to provide a complete connection url
  • username
  • password

If this property is set and the broker is already connected an error will be logged unless it has the force property set - in which case it will disconnect from the broker, apply the new settings and reconnect.