MQTT
# What is are MQTT nodes used for in Node-RED
Node-RED comes with 2 MQTT nodes.
# MQTT-in
The MQTT-in node is used to subscribe to a topic pattern on a given MQTT broker. This means messages published to a topic that matches the pattern will be delivered to the flow by this node.
# MQTT-out
The MQTT-out noe is used to publish messages to a topic from the flow.
# Configuring
Both of the nodes mentioned above make use a configuration node which holds all the details about connecting to a given MQTT broker. This includes things like:
- hostname
- port
- username/password
Node-RED supports MQTT v3.x and v5 as well as allowing connecting to brokers using both native MQTT and MQTT over WebSockets.
# Examples
Simple publish and subscribe
Cheerlights
Node Documentation
Connects to a MQTT broker and subscribes to messages from the specified topic.
Outputs
Details
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 Subscription
Inputs
These only apply when the node has been configured for dynamic subscriptions.
Connects to a MQTT broker and publishes messages.
Inputs
Details
msg.payload
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
Inputs
Configuration for a connection to an MQTT broker.
This configuration will create a single connection to the broker which can then be reused by MQTT In
and MQTT Out
nodes.
The node will generate a random Client ID if one is not set and the node is configured to use a Clean Session connection. If a Client ID is set, it must be unique to the broker you are connecting to.
Birth Message
This is a message that will be published on the configured topic whenever the connection is established.
Close Message
This is a message that will be published on the configured topic before the connection is closed normally, either by re-deploying the node, or by shutting down.
Will Message
This is a message that will be published by the broker in the event the node unexpectedly loses its connection.
WebSockets
The node can be configured to use a WebSocket connection. To do so, the Server field should be configured with a full URI for the connection. For example:
ws://example.com:4000/mqtt