# "mqtt in" node 'Best Practice'

**URL:** <https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333>\
**Category:** General\
**Created:** [9 January 2022 03:39 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333 "2022-01-09T03:39:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![MakerMan](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@MakerMan](https://discourse.nodered.org/u/MakerMan)\
**Post date:** [9 January 2022 03:39 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/1 "2022-01-09T03:39:35Z")

</div>

I've looked around and not spotted information about best practice approaches regarding the use of the `mqtt in` node.

There are two obvious approaches;

(a) In each flow where a particular message is needed, create a `mqtt in` subscribe node. Hence subscribing many times to the same data in the same Node-Red configuration.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/627e931ade858ba09a9c1be494946470f6cdbc4a.png)

(b) Subscribe to each message only once and send the output to a `link out` node. Re-use the message by consuming with as many `link in` nodes as needed.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/548ece16f79eb0de06fd775f322f8968d5fbef5d.png)

I was wondering which might be less resource intense.  
Is there any 'best practice' guidance in this area?

---

<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:** [9 January 2022 08:01 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/2 "2022-01-09T08:01:24Z")

</div>

> [@MakerMan](#):
>
> I was wondering which might be less resource intense

[Premature optimisation is the root of all evil](https://stackify.com/premature-optimization-evil/) 😉

* * *

IMO, option B is better.

The difference in processing time will be virtually identical but maintenance will be easier. Consider this...

With option A, if you ever needed to change the topic, you'd have to do this 3 (or more) times. With option B you would only need to do this once.

With option A, If you ever needed to temporarily disable this topic, you'd have to do that in 3 (or more) places. With option B you would only need to do this once.

With option B, you have only 1 source of truth. That is usually the better option.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 January 2022 08:24 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/3 "2022-01-09T08:24:44Z")

</div>

If you haven't read it yet, you might want to read the [MQTT Essentials - All Core Concepts explained](https://www.hivemq.com/mqtt-essentials/).

While it won't answer your initial question, it will give you more information about how to use MQTT. I like to sbscribe to the high level topic and then have a `switch` node examine the topic and send the messages where they should go.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 January 2022 08:38 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/4 "2022-01-09T08:38:00Z")

</div>

I tend to use option (b) because, as Steve pointed out - maintenance is much easier.  
Here's an example of some recent Room-Temperature-Display modules I've made as PCBs.

This is a simple dashboard showing three of the modules in the Study, Lounge and Garage.

 ![Screen Shot 01-09-22 at 08.26 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/5949f7b8af33ccb0ca6325fcf23a972e9b1f14c6.gif)

Here's part of the main flow.  
 ![Screen Shot 01-09-22 at 08.27 AM 001](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d48fcf9e9dca7648ca22cefb954171d63d806527.gif)

This is the format of the MQTT payload - notice the part called 'node\_number'  
 ![Screen Shot 01-09-22 at 08.27 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6bea93bc8eb53eda1bbf9eef4482e3b0573e534b.gif)

This is what one of the sub-flows looks like.  
 ![Screen Shot 01-09-22 at 08.28 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/e/5e2fb2a01ef4263ccf1118284271cb3469098fa9.gif)

The top function node filters out data for the lounge. e.g. node\_number == 32.  
 ![Screen Shot 01-09-22 at 08.44 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/496b9713a64e61481bf701f2effcfe978284e494.gif)

---

<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:** [9 January 2022 09:06 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/5 "2022-01-09T09:06:43Z")

</div>

I go for option A as I try to keep flows as independent as possible. I find lots of links between flows confusing. Also I try to keep functional independence for flows, so one flow handles one aspect of the system, with minimum coupling with other flows.  
So for example, I might have an Inputs flow, which collects data from sensors and outputs values to MQTT. The MQTT topics will be independent of sensor type. So for example if I measure a room temperature with a Zigbee device the value will come into the Inputs flow with a Zigbee topic, but I will send it out with a meaningful topic such as home/bedroom/temperature. If the sensor type were to change I only have to change this flow and it does not affect the rest of the system.  
Then I may have a dashboard flow that wants to show that temperature, so it will pick up home/bedroom/temperature and display it.  
In addition there will be a Control flow that picks up the temperature and drives the heating as appropriate.

---

<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:** [9 January 2022 09:44 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/6 "2022-01-09T09:44:36Z")

</div>

One important word of caution with option A: some broker implementations (not mosquitto) will send a separate message for each subscription for a topic. That in turn will cause each of those messages to be sent to each matching node in your flow. So you end up with multiple copies of each message being sent by each node.

Mosquitto thankfully doesn't so that, but it can catch people out when they try a different broker.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 January 2022 11:40 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/7 "2022-01-09T11:40:53Z")

</div>

> [@dynamicdave](#):
>
> Here's part of the main flow.

Just to put the cat among the pigeons - there is a fly in the optimisation ointment (I do _love_ to mix metaphors!). Doing it that way forces node.js to create 3 clones of the incoming message. For large, complex messages, this is actually relatively slow.

Of course, you wouldn't notice in most cases but if you were driving your platform hard or dealing with many messages per second, this could easily become a bottleneck.

> [@Colin](#):
>
> I go for option A as I try to keep flows as independent as possible.

One way to get the best of both worlds is to use **groups**. You can define an environment variable in a group and use that in your mqtt-in node. Meaning that you could then create 3 groups, possibly all using the same function node via link-call's.

If you have common functions, the link-call feature is great.

---

<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:** [9 January 2022 12:25 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/8 "2022-01-09T12:25:02Z")

</div>

> [@TotallyInformation](#):
>
> Doing it that way forces node.js to create 3 clones of the incoming message.

You'll end up with three copies of the message in either approach. It either gets cloned in the internals of the mqtt node, or it gets cloned where the flow branches.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [9 January 2022 12:58 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/9 "2022-01-09T12:58:17Z")

</div>

To me it just seemed the easiest and quickest way to do it.  
I'll willingly take and accept advice if there is a more efficient way to code it.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [9 January 2022 15:00 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/10 "2022-01-09T15:00:17Z")

</div>

To chime in, I use the option of a node for every topic. Like @Colin I feel it's easier to maintain and far less confusing. I have 24 topics/ nodes on one pi and each tab in the editor is a specific function. So if something goes wrong or trying to troubleshoot everything is right there. You can argue having a node or two on another tab is no big deal but I tried that way for a while and personally found it annoying.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [9 January 2022 17:04 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/11 "2022-01-09T17:04:02Z")

</div>

So is it fair to say that it doesn't make any difference in terms of performance / efficiency ?

---

<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:** [9 January 2022 17:40 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/12 "2022-01-09T17:40:56Z")

</div>

I would say it isn't worth losing sleep over. The choice will be entirely based on personal preference weighing up the pros/cons of maintaining a single entry point vs many. Performance/efficiency will not be a concern.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [9 January 2022 17:43 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/13 "2022-01-09T17:43:51Z")

</div>

Good to know - I have a mix of the two based on what works best in each case.

---

<div class="post-metadata">

**Author:** ![jmorris644](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jmorris644/32/31964_2.png) [@jmorris644](https://discourse.nodered.org/u/jmorris644)\
**Post date:** [9 January 2022 18:03 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/14 "2022-01-09T18:03:13Z")

</div>

Like @zenofmud I use a single MQTT in node and a switch to send the content along its merry way.

---

<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:** [10 March 2022 18:04 UTC](https://discourse.nodered.org/t/mqtt-in-node-best-practice/56333/15 "2022-03-10T18:04:09Z")

</div>

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