How-to
Set an MQTT broker address dynamically in Node-RED flows
Short answer
You cannot inject a broker address into a running MQTT node, because the address belongs to the shared mqtt-broker configuration node. For a fixed change, edit that configuration node in the editor and deploy. For switching at runtime, Node-RED 2.1 and later support the connect, disconnect, subscribe and unsubscribe actions.
On this page
You cannot inject a broker address into a running MQTT node in Node-RED: the address is not stored in the mqtt in or mqtt out node at all, so there is nothing for an http in message to overwrite. A fixed address change is made in the editor, on the shared mqtt-broker configuration node, and deployed. For switching at runtime, Node-RED 2.1 and later support the dynamic connect, disconnect, subscribe and unsubscribe actions, which control when the configured connection opens and closes. This article gives both paths, a worked changeover between two brokers, and the verification steps.
Why a message cannot set the broker address #
The MQTT in and out nodes each point at an mqtt-broker configuration node: a container defined in the editor that holds the address, port, credentials and TLS settings and has no runtime behavior of its own. An http in node can deliver a message whose payload carries an address, but no standard MQTT node reads a payload field as a new broker address, so the injection fails no matter how you wire it. That is deliberate: editing the configuration node moves every MQTT node that references it, and the editor is where that scope is visible. The configuration pane in the right-hand sidebar lists each configuration node together with the count of nodes that reference it — check that count before any change.
A search-and-replace on one mqtt out node's configuration is not a workaround either: the node itself stores no address to replace.
Change a fixed broker address in the editor #
- Open the configuration pane in the right-hand sidebar and note how many nodes reference the mqtt-broker configuration node you intend to change.
- If any of those nodes must stay on the old broker, give them their own configuration node first.
- Double-click any MQTT node that uses the configuration, then the pencil button beside the broker field, to open the configuration node itself.
- Enter the new address and port.
- Click Deploy. Every referencing node reconnects to the new address.
One edit and one deploy cover the whole flow — there is no per-node setting to hunt down afterwards.
Switch a connection at runtime on Node-RED 2.1 and later #
Dynamic MQTT connections arrived with Node-RED 2.1, released in October 2021. In the edit dialog of an MQTT in node, a dynamic subscription option appears; selecting it gives the node an input so messages can drive it. The actions the node accepts are connect, disconnect, subscribe and unsubscribe. They act on the connection state, not on the address: none of them accepts a new broker address, which is why the editor path above still owns any address change.
A function node ahead of the MQTT node sets the action on the message:
// illustrative function node wired before an MQTT out node
// msg is the standard Node-RED message object
msg.action = "disconnect";
return msg;
The subscribe and unsubscribe actions carry the topic to add or remove on the message itself, so one flow can widen or narrow its subscriptions while it stays connected.
Switching one flow from a lab broker at 192.168.1.50 to the plant broker at 10.0.10.20 #
Inputs for this example:
- The current configuration node (example name
lab-broker) holds the lab address 192.168.1.50 on port 1883, without TLS. It is the only broker configuration in the flow, which is the situation to watch. - The plant broker waits at 10.0.10.20 on the same port.
- Seven MQTT nodes reference
lab-broker: three mqtt in nodes plus four mqtt out nodes.
The rule this example applies: the dynamic actions govern the connection, and the address moves only through an editor edit plus a deploy.
The arithmetic that sizes the change: 3 + 4 = 7 nodes share the configuration, so one edit and one deploy move all seven connections at once. Nothing is left behind, provided no node was quietly given its own configuration — that is what the reference count in the first step of the editor path confirms.
The sequence: send the disconnect action and wait 2 seconds (an example pause) for the socket to close; open lab-broker, change its address to the plant value, and deploy; send the connect action; then publish a test message on plant/line1/heartbeat and confirm a subscriber receives it. To stand up the lab side of a test like this, the guide to running a secure Mosquitto MQTT lab broker with Node-RED covers a bench broker you can point at first.
Result: all seven nodes now connect to the plant broker. The decision: if the move is permanent, rename the configuration node, because a node called lab-broker that points at the plant describes nothing. If the move is temporary — a maintenance window, say — repeat the same sequence with the original address to switch back. And the threshold that guards the whole operation: the moment one of the seven nodes must stay on the old broker, stop and split it onto its own configuration node before editing the shared one.
Rehearse the sequence on a bench runtime before touching the line: a deploy that leaves the address wrong silences every node sharing that configuration until you correct it.
Verify the change #
- Watch the MQTT node's status indicator return to its connected state after the connect action.
- Publish a known test value and confirm it arrives at a subscriber on the other side; the page on how MQTT clients publish and subscribe through the broker walks that end-to-end path.
- Recheck the reference count: it should be unchanged, since you edited the configuration rather than re-pointing nodes.
- If the node stays offline after a correct address, the article on an MQTT sink that stays offline until you use the broker IP address covers the reachability causes to rule out.
- If the configuration uses TLS, remember the certificates live in the same configuration node: an address whose certificate chain does not match what the node expects fails the handshake, and the page on how TLS certificates secure a dynamic MQTT broker explains what the broker must present.
Questions asked
Can an http in node inject a new MQTT broker address at runtime?
No. The mqtt in and mqtt out nodes store no address of their own; the address lives in the shared mqtt-broker configuration node, and no standard node reads an incoming payload field as a new broker address. Change it in the editor and deploy, or use the Node-RED 2.1 dynamic connect and disconnect actions to control when the configured connection opens.
How do I tell whether my Node-RED supports dynamic MQTT connections?
Dynamic connections arrived with Node-RED 2.1. Open the edit dialog of an MQTT in node: if it offers the dynamic subscription option that adds an input to the node, your runtime supports the connect, disconnect, subscribe and unsubscribe actions. If the option is absent, upgrade before relying on them, because older runtimes have no message-driven connection control.
What happens to messages published while the connection is disconnected?
An MQTT out node cannot deliver anything while it is disconnected, and the documented dynamic actions include no buffering. Treat the switch window as a gap: stop the publishing flow before you send the disconnect action and restart it after the connect action is confirmed, or add your own store-and-forward stage ahead of the node.
Does editing the broker configuration node disturb other flows?
Every MQTT node that references the configuration node follows the change, including nodes on other tabs and in other flows. Check the reference count in the configuration pane before you edit, and retest each referencing node after the deploy, because a change made for one flow silently moves every connection that shares the configuration.