Dynamically set MQTT client ID

I know that the client ID can be set by environment variable, but it is inconvenient to do so. Each time user makes a change to the client ID, then Node-RED needs to restart. "Settings.js" file also needs to be adjusted.

Is it possible to change the client ID dynamically now? It looks like the client ID code is there (as shown below), but dynamically changing the client ID does not work at the moment from testing.

node.options.username = node.username;
            node.options.password = node.password;
            node.options.keepalive = node.keepalive;
            node.options.clean = node.cleansession;
            node.options.clientId = node.clientid || 'nodered_' + RED.util.generateId();

Yes. Send a connect action message with the new client id.

What have you tried?

It should be possible using action and broker props in a msg. Setup in a function as below:

msg.action = 'connect'
msg.broker = {
    clientid: 'my_different_id' // << set this however you wish
}
return msg

NOTE: you should send a disconnect action before attempting to run the connect action.

1 Like

I note that clientid is not mentioned in the help text for node, as far as I can see.

No. It's not. Only a subset is mentioned. Most properties can be dynamically set but there are lots of them & can have many different combinations and value combinations - the doc would be huge. I'm open to suggestions on this.

Where would one look to find out whether it should be clientID, clientId, clientid, etc?
Possibly at least the settings one can configure in the node should be included.

Most, if not all can be seen in the properties panel when you select a config node. The smaller panel with the name, id and other properties on the lower portion of the side bar.

Alternatively, in the src here: node-red/10-mqtt.js at 5d698d66d0857e65b08be41b212b7bcafe5f8261 · node-red/node-red · GitHub for about 100 lines

1 Like

Ahh, I used "clientId" instead of "clientid". That's the only difference.

Thanks much! It works beautifully now. :heart_eyes:

Perhaps a fuller list of properties and actions could be added to the online readme and a link to that provided in the help.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.