# Dynamically set MQTT client ID

**URL:** <https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825>\
**Category:** General\
**Created:** [30 May 2023 18:08 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825 "2023-05-30T18:08:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidz](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@davidz](https://discourse.nodered.org/u/davidz)\
**Post date:** [30 May 2023 18:08 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/1 "2023-05-30T18:08:55Z")

</div>

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.

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 May 2023 20:21 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/2 "2023-05-30T20:21:56Z")

</div>

> [@davidz](#):
>
> Is it possible to change the client ID dynamically now

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

> [@davidz](#):
>
> dynamically changing the client ID does not work at the moment from testing

What have you tried?

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/490d0aad64e583c3b6b680cbe8571a73a7270dd8.png)

```auto
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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2023 20:51 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/3 "2023-05-30T20:51:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 May 2023 20:53 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/4 "2023-05-30T20:53:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2023 20:59 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/5 "2023-05-30T20:59:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 May 2023 21:16 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/6 "2023-05-30T21:16:55Z")

</div>

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](https://github.com/node-red/node-red/blob/5d698d66d0857e65b08be41b212b7bcafe5f8261/packages/node_modules/%40node-red/nodes/core/network/10-mqtt.js#L474) for about 100 lines

---

<div class="post-metadata">

**Author:** ![davidz](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@davidz](https://discourse.nodered.org/u/davidz)\
**Post date:** [30 May 2023 21:43 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/7 "2023-05-30T21:43:11Z")

</div>

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

Thanks much! It works beautifully now. 😍

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 May 2023 09:11 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/8 "2023-05-31T09:11:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 June 2023 09:11 UTC](https://discourse.nodered.org/t/dynamically-set-mqtt-client-id/78825/9 "2023-06-14T09:11:32Z")

</div>

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