Hello.
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) {
node.status({});
var id = msg.payload.client_id;
const _host = msg.payload.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) {
node.status({fill:"green",shape:"dot",text:"connect"});
connections[id] = client; // check point
}
});// on connect
client.on('message', (_topic, _message) => {
msg.payload = {
topic : _topic,
message : JSON.parse(_message.toString())
};
node.send(msg);
});
client.on('reconnect', () => {
node.status({fill:"red",shape:"ring",text:"reconnect"});
});
client.on('offline', () => {
node.status({fill:"red",shape:"ring",text:"offline"});
});
client.on('disconnect', () => {
node.status({fill:"red",shape:"ring",text:"disconnect"});
});
client.on('error', (error) => {
node.status({fill:"red",shape:"ring",text:"error"});
});
}else{ // Topic Pub, When an MQTT connection already exists.
client = connections[id];
if (client.connected) {
if( _message ){
client.publish(_topic, JSON.stringify(_message));
node.status({fill:"green",shape:"dot",text:"connect"});
}else{
console.log("[MQTT] Message Empty");
}
}else{
delete connections[id];
node.status({fill:"red",shape:"ring",text:"disconnect"});
}
}//end if
});
this.on('close', function() {
try{
console.log("close event : First Call");
if (client) {
client.disconnect();
};
}catch(e){}
});
}
RED.nodes.registerType("mqtt-module", mqttFn);
}