Working With MQTT Dynamic Connections

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).

dynamic-connections
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

mqtt-dynamic-connections-1

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:

mqtt-dynamic-connections-2

Note: You should note that if you are already connected then you will first need to disconnect or you will get an error.

mqtt-dynamic-connections-error-3

However you can force a disconnect and reconnect with the new settings using the force setting.

force-dynamic-connection

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.

dynamic-subscriptions

When you do this an input port is added to the mqtt-in node. Now you can use this to subscribe as shown below or also to connect as shown for the publish node previously.

To subscribe dynamically you need to set the action property to subscribe and the topic property to the topic you want to subscribe to.

Dynamic-Subscription

To specify the QOS you need to send in an object as shown below:

 msg.topic={“topic”:”testtopic”,”qos”:0};
If you don’t specify the QOS a QOS of 2 is used by default.

To unsubscribe use the action unsubscribe and  set the topic that you want to unsubscribe from:

Dynamic-Unsubscribe



Demo Flow

To help you learn more about dynamic connections I have created a simple demo flow to illustrate the above.

Multiple Dynamic Connections

If you need to connect to multiple brokers dynamically then you need multiple nodes configured for dynamic connections.

This is no different than when connecting to multiple brokers using automatic connections as each connection needs a server node.

Summary

The MQTT nodes now allow for dynamic connections,dynamic broker settings and dynamic subscriptions make them much more flexible than in previous versions.

When setting the mqtt-in node for dynamic subscriptions and input port appears on the mqtt-in node.

Click to rate this post!
[Total: 5 Average: 5]

8 comments

      1. Hello Thanks for your reply.
        My clientID is dynamic as well as my topic… the broker to which I am sending uses also the client ID as an authentication to devices already registered.

        Can I patch this mqtt client? or do you know of any mqtt client contributed to node-red where I can set the clientid with javascript ?

        Rick,

        1. Rick
          Pass the client id as a query in the url as shown below

          //
          msg.action=”connect”;
          msg.topic=”test”;
          msg.broker={“broker”:”localhost”,”url”:”mqtt://localhost:1883?clientId=testclient”};
          return msg;
          //
          rgds
          steve

  1. Hello Steve,

    Thanks for this tutorial!
    I have a question can the client ID be also specified and not left to be autogenerated ?
    if yes can you please point out how ?

    regards
    Rick

  2. 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

    1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *