MQTT for problems that occur when singletones share the same object

I want to create a dynamically linked MQTT object.
I found 'node-red-contrib-mqtt-dynamictopic' as a similar node.
However, I ask because it is not like the function I want to make because only the Topic can be changed dynamically.

Create custom made.
You cannot configure MQTT with Properties.
Connection information is received dynamically.
All information of MQTT to connect to msg is passed.
I want to make it work to connect multiple MQTTs to multiple places.

I want to store and manage the ClientID Key in a shared MQTT array variable like Singletone.

A single MQTT works normally, but when multiple topics are connected to multiple servers, the MQTT connection offline - connect is repeated infinitely.

The source below is a brief summary of the code I wrote.
What kind of problem should I solve ('offline - connect ') to solve the recurring situation?

module.exports = function (RED) {

    var connections = {};   //topic : Client

    function mqttFn(config) {

        RED.nodes.createNode(this, config);

        var node = this;


        const mqtt = require('mqtt');

        let client = undefined;

        node.on('input', function (msg) {


            var id = msg.payload.client_id;

            const _host          =;

            const _port          = msg.payload.port;

            const _client_id     = msg.payload.client_id;

            const _root_ca       = msg.payload.root_ca;

            const _certificate   = msg.payload.certificate;

            const _private_key   = msg.payload.private_key;

            const _property_id   = msg.payload.property_id;

            const _topic         = msg.payload.topic;

            const _message       = msg.payload.message;

            const topic = `${_topic}/${_client_id}`;

            const options = {

                host: _host,

                port: _port,

                protocol: 'mqtts',

                clientId: _client_id,

                key: _private_key,

                cert: _certificate,

                ca: _root_ca                


            if (!connections[id]) {  //When an MQTT connection is not found, a new connection is created and saved.

                client = mqtt.connect(options);

                client.on('connect', () => {

                    client.subscribe(topic, function (err) {

                        if (!err) {

                            console.log("[MQTT] connect");



                    if (client.connected) {                        


                        connections[id] = client; // check point


                });// on connect

                client.on('message', (_topic, _message) => {

                    msg.payload = {

                        topic : _topic,

                        message : JSON.parse(_message.toString())




                client.on('reconnect', () => {




                client.on('offline', () => {



                client.on('disconnect', () => {




                client.on('error', (error) => {



            }else{ // Topic Pub,  When an MQTT connection already exists.

                client = connections[id];

                if (client.connected) {

                    if( _message ){

                        client.publish(_topic, JSON.stringify(_message));



                        console.log("[MQTT] Message Empty");



                    delete connections[id];



            }//end if            



        this.on('close', function() {



                console.log("close event : First Call");

                if (client) {






    RED.nodes.registerType("mqtt-module", mqttFn);


Often that is caused because you are using the same client id for multiple connections to the broker. Each connection must have a unique id. If you try to re-use an id with a different connection then the broker disconnects the first connection and connects the new one, then when the original client tries to use it again then it connects that one and disconnects the second one.

I already know that (connection-offline) is repeated when the Client ID is duplicated.
Currently, (connection-offline) is repeated when multiple msgs are sent by making the Client ID not to be duplicated.

var connections = {};

A server - MQTT
B server -MQTT
Z server-MQTT

It is saved in the object, but MQTT connection is disconnected in the process of adding MQTT.

The new Dynamic mode for the MQTT nodes in 2.1.0-beta.2 will allow you to dynamically control all aspects of the connection. I would suggest you test that out first before trying to implement your own version.

Dear knolleary
Thank you for answer.
I use v2.0.6 version.

I am making various Custom Nodes right now.
When a communication object is created and a specific communication object is connected to a listener
It is questionable whether all node.send(msg) is passed to the listener to handle the callback event.

The above question is simply an example of a question on MQTT.
I have a suspicion that the listener is not working properly for MQTT communication Instant from multiple servers.

Can you provide an easier guide to the above problem?