You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MQTT-Node: Messages are lost on connection problems, despite QOS-Level 1 #2864
Generate this simple testflow
Subscripe topic on another machine via mosquitto_sub -h <MQTT Server-Address> -t /testtopic -q 1
In the following 0;19 means 0am and 19 minutes. Thats just for better redoing the steps.
0:19 Click inject. You'll see that you receive the timestamp.
0:20 On NodeRed host do sudo iptables -A OUTPUT -d <IP of MQTT Server>/32 -j DROP to simulate network problems.
Click 5 times inject node.
Wait until MQTT Node shows connections problems
0:21 click 5 times inject node
0:22 do sudo iptables -D OUTPUT -d <IP of MQTT Server>/32 -j DROP to reestablish connection.
Don't wait. Immediately after executing that command click inject Node 5 times.
So a lot of messages are lost. It seems all messages are lost that are received while not in connected state.
What do you expect to happen?
I expected that all messages are received. Maybe duplicate because of QOS1, but definitely no dropped messages.
Or if dropped messages, then there should be thrown an error with you can catch by a Catch-Node.
However node queues as long as it thinks it is connected, but in fact due to iptables there is no connection anymore. So why can't it just queue up when it is disconnected?
When it is very hard to implement a queue there should be an error thrown which you can catch with a catch node as almost all nodes do....
On Raspberry PI this file is located at /usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js
Current (08.07.2021) it is in line 602. You find the line easily by searching for this.publish = function (msg,done) {
If you change it to evaluating always true if (node.connected || true) { messages get queued because of underlying library.
(If you want to change that in your installation the file is located at /usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js )
I've created a testflow which generates 2000 messages which are all delivered with modification while node MQTT node showed disconnected while receiving.
I've tried a bit farther. Even sending almost 10MB of JSON with this flow while connection is blocked works perfect. md5sum shows same result for input and output.
Yes - I'm aware of the option (although it didn't exist when these nodes were first written).
It isn't as simple as just removing that if statement however - as every existing flow will expect the current behaviour. We cannot just change that - it'll need to be an option exposed in the node.
I'm just finishing off the MQTTv5 support in these nodes. I'll take a look at adding in such an option
It isn't as simple as just removing that if statement however - as every existing flow will expect the current behaviour.
Can you give me an example? I expected when setting QOS 1 the behaviour as described here, messages gets queued and then sent when connection is re-established. Thats whats MQTT Level 1 is about. So to be hontest I was very astonished and shocked that these messages get silently dropped, because I expected them to be delivered no matter what network problems occur. When messages get dropped with QOS Level 1 set on connection problems than use of QOS 1 is very limited from my point of view.
With QOS 0 you're right. There should be an option not to queue these messages. Queuing them despite (the default npm mqtt implementation) is quite useful because it causes less overhead than QOS Level 1 but still all messages are transfered. Except those messages when connection drops right in the middle of transfering a message.
Do you have a timetable when you'll probably release the new MQTT version with message queuing in disconnected state?
The mqtt v5 support will be in 1.3 which we should have a beta release of in the next week or so.
Assuming there aren't any hidden problems, then I would hope this issue can also be addressed in that release. But until I look at it in detail, I won't commit to that.
Thanks. Thats very promising.
Any timetable how long it will take then until 1.3 is officially released and installed when you do a standard update of NodeRED?
The node does not send any messages if it is not currently connected. That is how it has always worked and has nothing to do with the MQTT spec. The spec is only concerned with the client-broker connection - it doesn't say anything about what the application driving the client is required to do.
I acknowledge it isn't ideal behaviour, but it is the behaviour we have.
Users may have created flows that use node status to track the connected state of the node and do their own buffering of messages. They may also rely on the fact the node discards messages during a disconnected state - regardless of the QoS setting.
If the node suddenly starts to queue messages internally, that is a significant change in behaviour that could break existing flows. It could lead to unexpected message delivery or memory issues.
So yes, it needs to be an option in the node to enable offline buffering of messages. There also then needs to be options over how large that buffer is allowed to get - and what should be done if that buffer limit is reached (eg, discard oldest vs discard newest).
OK.
I notice that if I pull the network cable then it takes two minutes on my Ubuntu system for the status to change to disconnected. Any messages I send during that time are queued and sent when the connection is re-established. I don't see any way of limiting the buffer used for those two minutes.
I suppose also there the question of what comprises the client and what comprises the app. As a user of node-red one tends to think of the node red MQTT node as the client rather than part of the app, but I suppose that it is the underlying mqtt module that is the client, and the node red node is the app, along with the flows that node-red is running.
Anyway, as long as we know what the plan is then that is the important thing.
To be honest, I think buffering messages in the offline state or on errors is a bad idea. From past experiences, such mechanisms are more error-prone than useful. Too many things to consider and to be taken into account (buffer sizes, error handling, harder to find errors, ...). So there should be at least an option to completely disable it.
A fail-fast behaviour is much more manageable on the client side. I think it would be better to improve the error handling, so that catch nodes can be triggered correctly and consistently, instead of silently dropping messages. That way, the user has full control over the correct action for his use-case.
Any update here? Is there a way to trigger a flow once mqtt is connected? Maybe via the new dynamic mqtt node? I tried using a status of the mqtt in node, it wasn't terrible (because the status.text is a translation key so shouldn't change), but not great.
@Steve-Mcl at the top of the page it states 4 of 5 tasks right next to the issue status. I assumed, that this means that 4 of 5 of the tasks neccessray to conclude this issue are done already?
that refers to the 5 check boxes that the person raising the issue can tick... and they only ticked 4 (missed the npm one) - so nothing to do with actual status of the issue per se.
Whats the current status? I've updated Node-RED and forgot to make the changes as described here #2864 (comment) so I lost data of a few hours.
There seems a lot of work going on, since this code is now in line 938 than 354 as before.
What about the idea just add a config flag "Queue messages" which is unchecked by default to have the same behaviour as before. If checked and QOS Level > 0 then messages will be queued like (this code is quick and dirty should be revised of course)
I'm currently running Node-RED v3.1.0 and I'm suffering the same issue. When the flows restart the other nodes fire before the MQTT connection is established and the first messages are lost. I have several sending nodes in my environment so building a complex queueing mechanism for each of them would be complex and redundant to just handle the restarts.
In v3.1.0 the lines described in comment #2864 are at line 1126-1127 of /usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:
node.publish = function (msg,done) {
if (node.connected) {
I added a || true to the if statement and it seems to solve the issue, but it would be really great to have a checkbox either in the out node or server config (better) so that future upgrades will not break things.
Hi,
Are there any updates here? I'm facing the same issues described by the others.
I find it kind of disappointing that the MQTT implementation of Node-RED suggests to handle incoming messages with QoS 1 accordingly, but behaves like a black hole for the messages sent to its input when the connection is currently down.
Is it realistic to have a way to opt-in to caching? Otherwise, I would be thankful for suggestions to prevent data-losses in my time-series use case.
I would be thankful for suggestions to prevent data-losses in my time-series use case.
You might like to look at @colinl/node-red-guaranteed-delivery which I have recently released, that includes an example showing how to use it to guarantee that MQTT messages have been successfully delivered to the broker.
Activity
knolleary commentedon Feb 24, 2021
The MQTT node does not queue up messages whilst it is disconnected. This is a very long standing limitation of the nodes.
It is something we should improve, but what you are observing has always been the behaviour of the nodes.
JsBergbau commentedon Feb 24, 2021
However node queues as long as it thinks it is connected, but in fact due to iptables there is no connection anymore. So why can't it just queue up when it is disconnected?
When it is very hard to implement a queue there should be an error thrown which you can catch with a catch node as almost all nodes do....
JsBergbau commentedon Feb 24, 2021
I've just taken a look into source code.
node-red/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js
Line 354 in 69befe8
Line 354 is the culprit
if (node.connected)On Raspberry PI this file is located at /usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js
Current (08.07.2021) it is in line 602. You find the line easily by searching for
this.publish = function (msg,done) {If you change it to evaluating always true
if (node.connected || true) {messages get queued because of underlying library.(If you want to change that in your installation the file is located at /usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js )
NodeRED uses npm mqtt library https://www.npmjs.com/package/mqtt
It has a built-in message queuing support. In fact its default setting is even to queue messages for QOS Level 0 on connection problems.
I've created a testflow which generates 2000 messages which are all delivered with modification while node MQTT node showed disconnected while receiving.
JsBergbau commentedon Feb 24, 2021
I've tried a bit farther. Even sending almost 10MB of JSON with this flow while connection is blocked works perfect. md5sum shows same result for input and output.
mosquitto_sub -h 192.168.178.79 -u <user> -P <password> -t /testtopic -q 1 > out.txtknolleary commentedon Feb 24, 2021
Yes - I'm aware of the option (although it didn't exist when these nodes were first written).
It isn't as simple as just removing that if statement however - as every existing flow will expect the current behaviour. We cannot just change that - it'll need to be an option exposed in the node.
I'm just finishing off the MQTTv5 support in these nodes. I'll take a look at adding in such an option
JsBergbau commentedon Feb 24, 2021
When you test, please be aware, that when receiver has QOS 1 defined message queue of mosquitto gets full quite fast. You have then to set
max_queued_messagesa lot higher, since it defaults only to 1000. Message bytes is by default not limited.https://mosquitto.org/man/mosquitto-conf-5.html
Can you give me an example? I expected when setting QOS 1 the behaviour as described here, messages gets queued and then sent when connection is re-established. Thats whats MQTT Level 1 is about. So to be hontest I was very astonished and shocked that these messages get silently dropped, because I expected them to be delivered no matter what network problems occur. When messages get dropped with QOS Level 1 set on connection problems than use of QOS 1 is very limited from my point of view.
With QOS 0 you're right. There should be an option not to queue these messages. Queuing them despite (the default npm mqtt implementation) is quite useful because it causes less overhead than QOS Level 1 but still all messages are transfered. Except those messages when connection drops right in the middle of transfering a message.
Do you have a timetable when you'll probably release the new MQTT version with message queuing in disconnected state?
knolleary commentedon Feb 25, 2021
The mqtt v5 support will be in 1.3 which we should have a beta release of in the next week or so.
Assuming there aren't any hidden problems, then I would hope this issue can also be addressed in that release. But until I look at it in detail, I won't commit to that.
JsBergbau commentedon Feb 25, 2021
Thanks. Thats very promising.
Any timetable how long it will take then until 1.3 is officially released and installed when you do a standard update of NodeRED?
knolleary commentedon Feb 25, 2021
Depends on what response the beta gets. Could be a couple weeks, could be a month. But unlikely to be much longer than that.
colinl commentedon Mar 3, 2021
Is this still planned for 1.3, or does that fact that it has been added to 2.0 planning mean it won't be till then?
knolleary commentedon Mar 3, 2021
This is planned for 2.0 at this point. After some investigation, it is much more work than adding a simple checkbox in the UI.
Given this is not a regression in behaviour, then we can't justifying delaying 1.3 even further for this.
colinl commentedon Mar 3, 2021
Does it need a checkbox? Isn't it a bugfix? The node is not conforming to the MQTT spec, or am I missing something?
knolleary commentedon Mar 3, 2021
The node does not send any messages if it is not currently connected. That is how it has always worked and has nothing to do with the MQTT spec. The spec is only concerned with the client-broker connection - it doesn't say anything about what the application driving the client is required to do.
I acknowledge it isn't ideal behaviour, but it is the behaviour we have.
Users may have created flows that use node status to track the connected state of the node and do their own buffering of messages. They may also rely on the fact the node discards messages during a disconnected state - regardless of the QoS setting.
If the node suddenly starts to queue messages internally, that is a significant change in behaviour that could break existing flows. It could lead to unexpected message delivery or memory issues.
So yes, it needs to be an option in the node to enable offline buffering of messages. There also then needs to be options over how large that buffer is allowed to get - and what should be done if that buffer limit is reached (eg, discard oldest vs discard newest).
colinl commentedon Mar 3, 2021
OK.
I notice that if I pull the network cable then it takes two minutes on my Ubuntu system for the status to change to disconnected. Any messages I send during that time are queued and sent when the connection is re-established. I don't see any way of limiting the buffer used for those two minutes.
I suppose also there the question of what comprises the client and what comprises the app. As a user of node-red one tends to think of the node red MQTT node as the client rather than part of the app, but I suppose that it is the underlying mqtt module that is the client, and the node red node is the app, along with the flows that node-red is running.
Anyway, as long as we know what the plan is then that is the important thing.
kuema commentedon Mar 3, 2021
To be honest, I think buffering messages in the offline state or on errors is a bad idea. From past experiences, such mechanisms are more error-prone than useful. Too many things to consider and to be taken into account (buffer sizes, error handling, harder to find errors, ...). So there should be at least an option to completely disable it.
A fail-fast behaviour is much more manageable on the client side. I think it would be better to improve the error handling, so that
catchnodes can be triggered correctly and consistently, instead of silently dropping messages. That way, the user has full control over the correct action for his use-case.5 remaining items
normanr commentedon Nov 7, 2021
Any update here? Is there a way to trigger a flow once mqtt is connected? Maybe via the new dynamic mqtt node? I tried using a status of the mqtt in node, it wasn't terrible (because the status.text is a translation key so shouldn't change), but not great.
dceejay commentedon Nov 7, 2021
Status is still your friend as that hasn’t changed
shurkanTwo commentedon Jun 9, 2022
Does anybody know when we can expect the 5th task of the ticket to be done? :)
Steve-Mcl commentedon Jun 9, 2022
@shurkanTwo
To what are you refering?
shurkanTwo commentedon Jun 9, 2022
@Steve-Mcl at the top of the page it states
4 of 5 tasksright next to the issue status. I assumed, that this means that 4 of 5 of the tasks neccessray to conclude this issue are done already?dceejay commentedon Jun 9, 2022
that refers to the 5 check boxes that the person raising the issue can tick... and they only ticked 4 (missed the npm one) - so nothing to do with actual status of the issue per se.

shurkanTwo commentedon Jun 9, 2022
@dceejay ah okay I see, thank's a lot! :)
So there has not been any progress so far?
JsBergbau commentedon Jun 10, 2023
Whats the current status? I've updated Node-RED and forgot to make the changes as described here #2864 (comment) so I lost data of a few hours.
There seems a lot of work going on, since this code is now in line 938 than 354 as before.
What about the idea just add a config flag "Queue messages" which is unchecked by default to have the same behaviour as before. If checked and QOS Level > 0 then messages will be queued like (this code is quick and dirty should be revised of course)
That would be a very efficient fix without breaking the current behaviour.
m4rc0d1 commentedon Dec 13, 2023
I'm currently running Node-RED v3.1.0 and I'm suffering the same issue. When the flows restart the other nodes fire before the MQTT connection is established and the first messages are lost. I have several sending nodes in my environment so building a complex queueing mechanism for each of them would be complex and redundant to just handle the restarts.
In v3.1.0 the lines described in comment #2864 are at line 1126-1127 of /usr/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:
I added a
|| trueto the if statement and it seems to solve the issue, but it would be really great to have a checkbox either in the out node or server config (better) so that future upgrades will not break things.NorbNorb commentedon Apr 23, 2024
Hi,
Are there any updates here? I'm facing the same issues described by the others.
I find it kind of disappointing that the MQTT implementation of Node-RED suggests to handle incoming messages with QoS 1 accordingly, but behaves like a black hole for the messages sent to its input when the connection is currently down.
Is it realistic to have a way to opt-in to caching? Otherwise, I would be thankful for suggestions to prevent data-losses in my time-series use case.
colinl commentedon Apr 23, 2024
You might like to look at @colinl/node-red-guaranteed-delivery which I have recently released, that includes an example showing how to use it to guarantee that MQTT messages have been successfully delivered to the broker.
hlovdal commentedon Dec 18, 2024
Steve Cope has a video Node-Red -MQTT Message Buffer (flow download) where he shows how to do buffering using function nodes.
Update system time as part of starting image acquisition (#590)