I use several MQTT signals in my flow.
What is good practice :
- Use a single multiple MQTT-IN node and filter out the required MQTT messages afterward
- Use multiple MQTT-IN nodes
I am inclined to think that using a single MQTT-In node is more resource efficient, but a lot of nodes downstream will be kept busy by filtering out unwanted MQTT messages.
I prefer the multiple MQTT-IN nodes because it makes the whole flow tidier and easy to manage.
I am not sure about @bakman2's comment in that post. I thought that there would only be one connection to the broker for each MQTT config node, so if multiple MQTT nodes share the same server config node there would be only one connection, with multiple subscriptions.
@GammaKappa do whatever you prefer, don't worry about efficiency unless you are handling thousands of messages per second. I prefer to use multiple MQTT nodes as it keeps the flows simpler. The simpler the flow the easier it is to understand and the less likely there are to be bugs.
In my case I am limited to the number of connections I can have to my broker.
So your first example would take up 3 connections to your broker.
The second example would only take 1 connection.
Just somthing to think about.
I don't think that is correct. I think there is only one connection for each mqtt server config node. In both cases there will be just one of those.
Easy enough to check with your broker, add more MQTT In nodes than you are allowed connections and see what happens. Make sure they all use the same server config node.
In fact I believe I can see that I am correct. On one of my PIs I have mosquitto installed as a local broker. When I look in config nodes I can see the mqtt config node
which tells me that I have 105 MQTT nodes using the broker.
When I look in the node-red startup log I can see that there is just one connection initiated to the broker
Dec 28 11:17:36 owl2 Node-RED: 28 Dec 11:17:36 - [info] [mqtt-broker:3f2f21cc.26840e] Connected to broker: owl2@mqtt://localhost:1883
OK, I must be mistaken. I did not mean to give misinformation.
Thanks for correcting me.
Well since you cannot have 2 MQTT clients with the same name, and those 105 nodes are all using the same client name set in the config, there must only be 1 connection to the broker.
Just to make sure - netstat output
tcp 0 0 localhost:1883 localhost:37880 ESTABLISHED 584/mosquitto
This question pops up quite often - just making sure there is no doubt
First, thank you all for your helpful comments here. I am in the process of moving my system over to pure MQTT, and this has become relevant very quickly.
For anyone else who finds this thread via search, I would like to make the case for using just one In and one Out.
What happens if you have to change brokers?
Both In and Out nodes let you choose brokers, sure, but editing two nodes to switch to a new config has to be vaaaaastly less effort than editing 60+ individual nodes to the new config. Although I guess you could edit the old MQTT config with the new info as a workaround, now that I think about it.
In any case, what I'm doing is somewhere in the middle. I use a single MQTT-in node per flow, because I use wildcards to make sure I'll only be looking at, for example, messages that have "fan" in the topic somewhere, then I stick a link-out node after it.
I pipe that all over the flow using link-in nodes for each place that needs to subscribe to relevant MQTT topics.
For publishing, though, I have a single MQTT-OUT node for all flows that has a Link-in on it, so that everything can use it.
Keeps things clean (for me) so far. I'm sure there's better ways of doing it; that's just where I am.