# MQTT dynamic setting

**URL:** https://discourse.nodered.org/t/mqtt-dynamic-setting/52607
**Category:** Feature Requests
**Created:** [23 October 2021 08:46 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607 "2021-10-23T08:46:11Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![ryeger](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ryeger/32/26789_2.png) [@ryeger](https://discourse.nodered.org/u/ryeger)
#### Post date: [23 October 2021 08:46 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/1 "2021-10-23T08:46:11Z")

</div>

Hello,  
Regarding the dynamic subscription of MQTT from version 2.1.0, I've noticed that if you feed a MQTT node with several topics for subscription (one after another as separate msg's), the node will "remember" the topics and act well.  
But, there is no way to know to what topics the node is already subscribed (unless the user takes care of this elsewhere). What I mean is that the node does not have a report status which it can output all of its subscribed topics (and maybe some more info).

One way I thought to deal with this is to remove all subscribed topics by sending a msg with action 'unsubscribe' and with topic '#" but this doesn't work.

Would like to know if there is a way to clear all subscribed topics without knowing what they are...

Thanks, Rami

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [23 October 2021 09:36 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/2 "2021-10-23T09:36:33Z")

</div>

There is a _currently undocumented_ feature that lets you send `msg.action="getSubscriptions"` and the node will send a message with:

```auto
{
   topic: "subscriptions",
   payload: [.. array of subscription objects ..]
}

```

You can then pass that back into the node having moved `msg.payload` to `msg.topic` and set `msg.action="unsubscribe"`

---

<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: [23 October 2021 12:17 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/3 "2021-10-23T12:17:52Z")

</div>

> [@ryeger](#):
>
> One way I thought to deal with this is to remove all subscribed topics by sending a msg with action 'unsubscribe' and with topic '#" but this doesn't work.
> 
> Would like to know if there is a way to clear all subscribed topics without knowing what they are..

You should also be able to send `action` "unsubscribe" & `topic` `true` (not tested, not sure if that feature made the cut but it was there before release).

---

<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: [23 October 2021 12:20 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/4 "2021-10-23T12:20:43Z")

</div>

> [@ryeger](#):
>
> remove all subscribed topics by sending a msg with action 'unsubscribe' and with topic '#"

That would be easy to achieve & might make more sense than sending `true` what do you think @knolleary?

---

<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: [23 October 2021 14:24 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/5 "2021-10-23T14:24:02Z")

</div>

Is it possible to subscribe to `#` and also other topics (strange I know, but if I tried hard enough I could probably think of a use case). In that case unsubscribing from `#` should only remove that one.

---

<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: [23 October 2021 14:25 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/6 "2021-10-23T14:25:48Z")

</div>

hmmm, yeah, good point.

it is (should be) possible to subscribe to '#' (though some brokers dont allow you or just dont transmit anything to '#')

---

<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: [23 October 2021 14:28 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/7 "2021-10-23T14:28:29Z")

</div>

> [@Steve-Mcl](#):
>
> some brokers dont allow you

Are you sure? Mosquitto certainly allows it.

---

<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: [23 October 2021 14:50 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/8 "2021-10-23T14:50:03Z")

</div>

I use EMQX broker and by default it doesnt give you anything for '#' - I believe it can be changed in the settings.

---

<div class="post-metadata">

### Author: ![ryeger](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ryeger/32/26789_2.png) [@ryeger](https://discourse.nodered.org/u/ryeger)
#### Post date: [23 October 2021 14:54 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/9 "2021-10-23T14:54:36Z")

</div>

Great, this is a good and elegant solution  
Thanks

---

<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: [23 October 2021 14:58 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/10 "2021-10-23T14:58:35Z")

</div>

I dont know if you missed it but simply injecting `true` will remove all subscriptions...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db7b616dcfc7a3e6bd831af8aa8e9c1f615cf214.png)

---

<div class="post-metadata">

### Author: ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)
#### Post date: [23 October 2021 18:07 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/11 "2021-10-23T18:07:32Z")

</div>

I am sure it is just a lack of imagination on my part, but as a regular user of the MQTT nodes before this addition, do you have a sample case where it would make since to use the dynamic setting capability?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [23 October 2021 18:27 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/12 "2021-10-23T18:27:17Z")

</div>

There are a number of scenarios that were hard to do without the dynamic mode.

The following are scenarios that I've come across from time to time

- running on a device with intermittent connectivity and wanting to schedule the connect/disconnect, rather than being 'always' connected

- connecting to a service that dynamically provisions the connection details. Such as having a standard set of flows that are deploy to multiple machines, and each machine has to "call home" to a provisioning service to get the broker details it should connect to.

- an mqtt service that rotates credentials regularly so you cannot hard-code them in the flow

- a service that has to 'discover' it's topic tree based on events so cannot be hardcoded

It is certainly a feature that most mqtt users won't need... Until you need it, then you _really_ need it.

---

<div class="post-metadata">

### Author: ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)
#### Post date: [23 October 2021 18:30 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/13 "2021-10-23T18:30:49Z")

</div>

Thanks for the answer. I think the last two are ones I can really imagine clearly now that you mention them. I think you hit the nail on the head with your final part:

> [@knolleary](#):
>
> It is certainly a feature that most mqtt users won't need... Until you need it, then you _really_ need it.

Great that the continued development is getting to these more obscure corner cases that won't be needed often, but will be very valuable when they are.

---

<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: [22 December 2021 18:31 UTC](https://discourse.nodered.org/t/mqtt-dynamic-setting/52607/14 "2021-12-22T18:31:22Z")

</div>

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