# MQTT V5 performance: dynamic subscription VS single topic

**URL:** https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798
**Category:** General
**Created:** [11 June 2022 17:12 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798 "2022-06-11T17:12:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)
#### Post date: [11 June 2022 17:12 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798/1 "2022-06-11T17:12:29Z")

</div>

Hi, is there someone that can explain how connections works?  
I mean, in terms of broker's load (but even regarding node-red ), is there a difference if I subscribe via two MQTT-in node ore just via a dynamic one?  
This because with the single topic I can check to not receive messages published by the same client, while I cannot do it with the dynamic one. So, in this last case, I have to add a topic level in order to differentiate the messages sent by the same client.  
Since I have lots of messages each second, I wanted to understand which is the best solution.

---

<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: [11 June 2022 18:40 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798/2 "2022-06-11T18:40:27Z")

</div>

> [@Lupin\_III](#):
>
> This because with the single topic I can check to not receive messages published by the same client, while I cannot do it with the dynamic one

You can.

When you subscribe dynamically, the topic can be a string, object, or an array:

Subscribe 1 (single string)

```auto
msg.action = "subscribe"
msg.topic = "testing_dyn_con/test1"

```

Subscribe 2 (strings)

```auto
msg.action = "subscribe"
msg.topic = ["testing_dyn_con/test1", "testing_dyn_con/test2"]

```

Subscribe 3 (using strings and options)

```auto
msg.action = "subscribe"
msg.topic = [
  {
    "topic": "testing_dyn_con/test1",
    "nl": true, //no local - Do Not Receive Messages Published By This Client 
    "ral": true, //Keep retain flag of original publish (same as 'mqtt in' default)
    "rh": 0, //Retained Message Handling: 0 = Send retained messages, 1= Only send for new subscriptions, 2= Do not send
  },
  {
    "topic": "testing_dyn_con/test2",
    "qos": 2,
    "nl": false,
  },
  "testing_dyn_con/test3"
]

```

---

<div class="post-metadata">

### Author: ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)
#### Post date: [12 June 2022 08:21 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798/3 "2022-06-12T08:21:03Z")

</div>

Thanks so much @Steve-Mcl. I didn't know this way of setting options with dynamic subscription. I thought I could set just Topic and QoS. Exactly what I was looking for.  
Ok, now in order to avoid bottlenecks and better understand how MQTT / node-red connections works, it is correct to say that:

- any connection channel is creates when I set the server tab (username, password and ID client)
- so, all messages with the subscribed topics are delivered by this channel.
- even if I use more mqtt-in node, all set with the same server parameter (selecting it by the menù and no creating a new one each time), the connection channel is always the same. So, just one.
- in this case there is no difference as workload in use more single MQTT-in or just one as a dynamic subscription

Is that correct?

---

<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: [12 June 2022 08:35 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798/4 "2022-06-12T08:35:30Z")

</div>

Not certain that translates well (sorry).

I will answer what I _think_ you are asking (hopefully you can pick out an answer)...

> [@Lupin\_III](#):
>
> any connection channel is creates when I set the server tab (username, password and ID client)

Not sure I understand this statement/question?  
When you create a broker connection (MQTT config) it is one single connection. You can (should) set it to **NOT** AUTO CONNECT - then at a later time, send an action to connect

> [@Lupin\_III](#):
>
> so, all messages with the subscribed topics are delivered by this channel.

If I understand, you are asking _"All topics subscribed via 1 MQTT-IN node are emitted by the same MQTT-IN node only?"_ - that is correct ✅  
(See ramblings below for more)

> [@Lupin\_III](#):
>
> even if I use more mqtt-in node, all set with the same server parameter (selecting it by the menù and no creating a new one each time), the connection channel is always the same. So, just one.

Yes, just one connection to the broker ✅

> [@Lupin\_III](#):
>
> in this case there is no difference as workload in use more single MQTT-in or just one as a dynamic subscription

No measurable difference.

  

Some ramblings that might help...

- If you create 1 MQTT-Broker config & have 1, 2, 5, 100 additional MQTT-IN nodes, they will ALL use the same SINGLE connection.
- If you disconnect this single MQTT-Broker (`msg.action="disconnect"`) ALL nodes that use this connection will become disconnected.
- Any topic you dynamically subscribe to will only emit events from THAT node

---

<div class="post-metadata">

### Author: ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)
#### Post date: [12 June 2022 20:51 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798/5 "2022-06-12T20:51:03Z")

</div>

Thanks @Steve-Mcl, I think I got the answers.  
Pratically I wanted to understand if once I set the server (broker) parameters within the dedicated tab, selecting always this configuration at the end the broker connection will be just one, no matter how many mqtt-in node I use.  
So even if I use 10 mqtt-in node, all with the same broker configuration, the broker connection will be just one and all the messages will be delivered trough this one single connection.  
From your reply I understand that yes, it works like this  
Thanks

---

<div class="post-metadata">

### Author: ![9toejack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/9toejack/32/56868_2.png) [@9toejack](https://discourse.nodered.org/u/9toejack)
#### Post date: [13 June 2022 06:00 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798/6 "2022-06-13T06:00:59Z")

</div>

something elsse to think about. @Lupin_III, if you want a single topic displayed in 2 locations, you dont need to set up another mqtt node. you can simply use a link node out from your Mqtt node. and then on what ever flow tab you are trying to send that info to you use a link in.  
here is a silly example but effective

```auto
[{"id":"9f0e6c0aabf5d34f","type":"inject","z":"ef1fc105685aed46","name":"FakeMqtt","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"just a test","payload":"","payloadType":"date","x":615.6666870117188,"y":609,"wires":[["3c04179f41b36ca6"]]},{"id":"3c04179f41b36ca6","type":"link out","z":"ef1fc105685aed46","name":"","mode":"link","links":["8708e448cf096cae"],"x":812.6666870117188,"y":608,"wires":[]},{"id":"8708e448cf096cae","type":"link in","z":"ef1fc105685aed46","name":"","links":["3c04179f41b36ca6"],"x":675.6666870117188,"y":678,"wires":[["ede0226fddbda16b"]]},{"id":"ede0226fddbda16b","type":"debug","z":"ef1fc105685aed46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":789.6666870117188,"y":677,"wires":[]}]

```

cheers!

---

<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: [12 August 2022 06:01 UTC](https://discourse.nodered.org/t/mqtt-v5-performance-dynamic-subscription-vs-single-topic/63798/7 "2022-08-12T06:01:57Z")

</div>

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