# Efficiently subscribe to multiple MQTT topics using one MQTT node

**URL:** https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527
**Category:** General
**Created:** [22 July 2020 06:53 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527 "2020-07-22T06:53:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)
#### Post date: [22 July 2020 06:53 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/1 "2020-07-22T06:53:55Z")

</div>

Hey. I need to subscribe to multiple MQTT topics using a single MQTT node. Is that a good way to simplify the flow as I do not want to create a MQTT subscribtion node for every remote device that I have.

I might have 50 remote devices that I need to listen to, and I certainly dont want to create 50 individual MQTT nodes.

```auto
[{"id":"ff173687.543bf8","type":"mqtt in","z":"df177cb8.27882","name":"","topic":"device1/number_to_pick","qos":"2","datatype":"auto","broker":"2727c5a5.a4fb6a","x":350,"y":400,"wires":[["c5854ac5.fde828"]]},{"id":"9e91a191.4a7a9","type":"debug","z":"df177cb8.27882","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":400,"wires":[]},{"id":"c5854ac5.fde828","type":"function","z":"df177cb8.27882","name":"Generate SQL","func":"//add the values to the msg so they can be debugged and used later\nquantity = msg.payload\n\n//msg.topic=\"UPDATE pack_to_light SET Quantity = 0 WHERE Device = 'device1'\"\nmsg.topic=`UPDATE pack_to_light SET Quantity = '${quantity}' WHERE Device = 'device1'`\n//msg.topic=`REPLACE INTO pack_to_light (Device,Item,Serial) VALUES ('${msg.device}','${msg.item}',${msg.serial})`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":400,"wires":[["9e91a191.4a7a9","97fe0f98.1e2a3"]]},{"id":"97fe0f98.1e2a3","type":"mysql","z":"df177cb8.27882","mydb":"97be0e96.67231","name":"Add item to database","x":800,"y":480,"wires":[[]]},{"id":"2727c5a5.a4fb6a","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"workshop/status","birthQos":"1","birthRetain":"true","birthPayload":"online","closeTopic":"workshop/status","closeQos":"1","closeRetain":"true","closePayload":"offline","willTopic":"workshop/status","willQos":"1","willRetain":"true","willPayload":"disconnected"},{"id":"97be0e96.67231","type":"MySQLdatabase","z":"","name":"","host":"localhost","port":"3306","db":"test","tz":""}]

```

So this is a flow for 1 MQTT subscribe node. I am just listening and ocasionally a device sends a message. After receiving a message, I need to update MYSQL table.

 ![2020-07-22-095433_1920x1080_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/4/849e11995a92ac3df452a87bff8b04f914e28d08.png)

```auto
quantity = msg.payload

msg.topic=`UPDATE pack_to_light SET Quantity = '${quantity}' WHERE Device = 'device1'`

return msg;

```

How can I change the current nodes to listen for lets say 20 devices? (devices1,devices2,devices3,devices4 .......) As you can see now, I am only listening to one device:  
device1/number\_to\_pick

Also, what would happen if multiple devices send a mqtt message at the same time?

---

<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: [22 July 2020 07:11 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/2 "2020-07-22T07:11:22Z")

</div>

You can use wildcards + and #

If you had structured your topics `device/1/xxxxx`, `device/2/xxxxx`, `device/n/xxxxx` etc, you could subscribe to `device/+/xxxxx`

As it stands, you _could_ subscribe to `+/xxxxx` but then you will also get `bedroom/xxxxx`, `garage/xxxxx` etc.

If that is not an issue, then there is a solution.

#### ramblings...

This is something that often hits people later down the line, who didnt read up on MQTT topic best practices. (not saying that is the case for you - just illustrating my point below)

---

<div class="post-metadata">

### Author: ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)
#### Post date: [22 July 2020 07:14 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/3 "2020-07-22T07:14:41Z")

</div>

I assume I would need to change the structure of my topics for this solution since I have arranged them as following:

device1/topic1  
device1/topic2

device2/topic1  
device2/topic2

device3/topic1  
device3/topic2

I only need to subscribe to "number\_to\_pick" topic for all devices

---

<div class="post-metadata">

### Author: ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)
#### Post date: [22 July 2020 07:18 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/4 "2020-07-22T07:18:49Z")

</div>

I have downloaded digitaloak-mqtt library which allows an input to be connected to mqtt subscribe node ( for the original mqtt subscribe node you cannot do that).

I would assume this would not work?

 ![2020-07-22-101822_1920x1080_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/987400a0e93395de8a9233b1afd686646454fcfa.png)

---

<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: [22 July 2020 07:30 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/5 "2020-07-22T07:30:27Z")

</div>

> [@zazas321](#):
>
> I assume I would need to change the structure of my topics for this solution since I have arranged them as following

Not if you subscribe to `+/number_to_pick` and you will get all 4 - however, my point was if you have `kitchen/number_to_pick` you will ALSO get that. Thats why topic convention should be good.

> [@zazas321](#):
>
> I have downloaded digitaloak-mqtt library which allows an input to be connected to mqtt subscribe node

That is an option but it is still multiple subscriptions under the hood. It is better to have good topic structure.

Look into `homie convention` and alternative `mqtt topic conventions` - they have put far more consideration into topic structure than I can express here.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [22 July 2020 08:30 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/6 "2020-07-22T08:30:47Z")

</div>

You can use one mqtt node with a topic of `+/#` you will get everything, you still need to handle all devices yourself, but as @Steve-Mcl indicated - better set it up with a certain convention.

I have root topics based on their source/location and in there grouped by device, ie.

`homebridge/to/*`  
`homebridge/from/*`  
`devices/livingroom/led_strip1/*`  
..etc

Pretty sure you can make a whole study out of it, each device as a root topic does not seem really flexible.

---

<div class="post-metadata">

### Author: ![zazas321](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zazas321](https://discourse.nodered.org/u/zazas321)
#### Post date: [22 July 2020 09:27 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/7 "2020-07-22T09:27:02Z")

</div>

I got it. Thank you for you help guys!

---

<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: [20 September 2020 09:27 UTC](https://discourse.nodered.org/t/efficiently-subscribe-to-multiple-mqtt-topics-using-one-mqtt-node/30527/8 "2020-09-20T09:27:10Z")

</div>

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