Prior to node-red version 2.1 an MQTT node would auto connect when the flow was started and it was not possible to disconnect from a broker.
In addition the subscribe node was configured with a topic, and it wasn’t possible to subscribe to additional topics or unsubscribe from topics once the flow had started.
The v2.1 release of node-red now as support for
- dynamic connections
- dynamic broker settings
- dynamic subscriptions
giving you the ability to control connection and subscriptions programmatically.
Configuring Dynamic Connections
The server connection dialogue now has a check box for dynamic connections (connect automatically).
The connect automatically check box is usually enabled by default. in which case the node functions as it did before and will connect automatically and you cannot disconnect.
By disabling it you set the node for dynamic connections.
When an MQTT node is configured for a dynamic connection you will need to connect and disconnect manually.
In addition you can also change broker configuration without editing the node.
The actual broker details can be passed dynamically to the MQTT-IN and MQTT-OUT nodes and will overwrite the settings configured in the node configuration.
You can set the broker, port, username, password.
How it Works
Remember that the MQTT-IN Node (subscribe) and MQTT-Out (publish) nodes can share a broker connection.
If you have a MQTT subscribe node and a publish node in a flow and you have configured them to use the same broker and to use dynamic connections then sending a connect signal to either node will cause both nodes to connect as they are sharing the same broker connection.
The connect/disconnect control message is passed into the node using the action property (msg.action).
The screen shot below shows an inject node configured to send the connect message
You should notice that no payload is sent but if you do send a payload it is not sent.
When dynamic connections are enabled the node server settings can also be changed by injecting a message with new settings.
The broker settings are sent as a JavaScript object in the msg.broker property.
The screen shot below shows the the inject node configured to send a connect message and also change the broker settings:
Note: You should note that if you are already connected then you will first need to disconnect or you will get an error.
However you can force a disconnect and reconnect with the new settings using the force setting.
Dynamic Subscriptions
With the old MQTT-IN node subscribing was automatic and you could not unsubscribe. The topic was set as part of the node configuration.
The publish node always allowed the topic to be sent into the node from a previous node.
You can now do this with the subscribe node by enabling dynamic subscriptions.
To subscribe dynamically you need to set the action property to subscribe and the topic property to the topic you want to subscribe to.
To unsubscribe use the action unsubscribe and set the topic that you want to unsubscribe from:
Demo Flow
To help you learn more about dynamic connections I have created a simple demo flow to illustrate the above.
Summary
The MQTT nodes now allow for dynamic connections,dynamic broker settings and dynamic subscriptions make them much more flexible than in previous versions.
- How to Install and Run The Aedes/mosca Broker on Node-Red
- Configuring the Node-Red MQTT Publish and Subscribe Nodes
- Using the HTTP Request Node
Hi Steve,
I have not found a way to connect dynamically to a TLS encrypted server. Is it possible?
msg.broker should have an option “tls”: true…
Am I overseeing something?
Best
Steffen
Yes there is a tls button under the connect dynamically button. Click it and add new tls configuration.
I haven’t tried it but don’t see why it shouldn’t work. Let me know if you have problems and I will test it.
Rgds
Steve