Skip to content

MQTT-Node: Messages are lost on connection problems, despite QOS-Level 1 #2864

Description

@JsBergbau

What are the steps to reproduce?

Generate this simple testflow
grafik
grafik
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
grafik

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.

What happens?

MQTT Client shows

1612739965989 --> 0:19
1612740005045 --> 0:20
1612740006033 --> 0:20
1612740006913 --> 0:20
1612740007707 --> 0:20
1612740008476 --> 0:20

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.

Please tell us about your environment:

  • Node-RED version: v1.2.6
  • Node.js version: v12.20.0
  • npm version:
  • Platform/OS: Linux 5.4.79-v7l+ arm LE
  • Browser: Firefox

Activity

  1. knolleary commented on Feb 24, 2021

    @knolleary
    Member

    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.

  2. JsBergbau commented on Feb 24, 2021

    @JsBergbau
    Author

    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....

  3. JsBergbau commented on Feb 24, 2021

    @JsBergbau
    Author

    I've just taken a look into source code.


    Line 354 is the culprit
    if (node.connected)

    this.publish = function (msg,done) {
                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.

    grafik

    I've created a testflow which generates 2000 messages which are all delivered with modification while node MQTT node showed disconnected while receiving.

    [{"id":"f1b85ea2.84c608","type":"mqtt out","z":"e82c1d84.6da8","name":"","topic":"/testtopic","qos":"1","retain":"","broker":"e30d744f.9a1ae","x":980,"y":580,"wires":[]},{"id":"25333456.7145c4","type":"function","z":"e82c1d84.6da8","name":"","func":"\nfor(var i = 1; i<=2000; ++i)\n{\n    msg.payload = i;\n    node.send(msg);\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":580,"wires":[["f1b85ea2.84c608"]]},{"id":"616c5428.6380b4","type":"inject","z":"e82c1d84.6da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":490,"y":580,"wires":[["25333456.7145c4"]]},{"id":"e30d744f.9a1ae","type":"mqtt-broker","name":"192.168.178.79","broker":"192.168.178.79","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
    
  4. JsBergbau commented on Feb 24, 2021

    @JsBergbau
    Author

    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.

    grafik

    [{"id":"612766d6.5da738","type":"file in","z":"e82c1d84.6da8","name":"","filename":"/home/pi/9800KBJSON.txt","format":"lines","chunk":false,"sendError":false,"encoding":"none","x":770,"y":740,"wires":[["43052a21.bce0c4"]]},{"id":"fc483699.e85de8","type":"inject","z":"e82c1d84.6da8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":460,"y":740,"wires":[["612766d6.5da738"]]},{"id":"43052a21.bce0c4","type":"json","z":"e82c1d84.6da8","name":"","property":"payload","action":"","pretty":false,"x":1190,"y":740,"wires":[["846dc0c1.9edee"]]},{"id":"846dc0c1.9edee","type":"mqtt out","z":"e82c1d84.6da8","name":"","topic":"/testtopic","qos":"1","retain":"","broker":"e30d744f.9a1ae","x":1360,"y":740,"wires":[]},{"id":"e30d744f.9a1ae","type":"mqtt-broker","name":"192.168.178.79","broker":"192.168.178.79","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
    

    grafik

    mosquitto_sub -h 192.168.178.79 -u <user> -P <password> -t /testtopic -q 1 > out.txt

     md5sum out.txt
    182499b829d53cfcbbd6cb66ffa5117c  out.txt
    
  5. knolleary commented on Feb 24, 2021

    @knolleary
    Member

    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

  6. JsBergbau commented on Feb 24, 2021

    @JsBergbau
    Author

    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_messages a lot higher, since it defaults only to 1000. Message bytes is by default not limited.
    https://mosquitto.org/man/mosquitto-conf-5.html

    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?

  7. knolleary commented on Feb 25, 2021

    @knolleary
    Member

    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.

  8. JsBergbau commented on Feb 25, 2021

    @JsBergbau
    Author

    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?

  9. knolleary commented on Feb 25, 2021

    @knolleary
    Member

    Depends on what response the beta gets. Could be a couple weeks, could be a month. But unlikely to be much longer than that.

  10. colinl commented on Mar 3, 2021

    @colinl
    Contributor

    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?

  11. knolleary commented on Mar 3, 2021

    @knolleary
    Member

    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.

  12. colinl commented on Mar 3, 2021

    @colinl
    Contributor

    Does it need a checkbox? Isn't it a bugfix? The node is not conforming to the MQTT spec, or am I missing something?

  13. knolleary commented on Mar 3, 2021

    @knolleary
    Member

    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).

  14. colinl commented on Mar 3, 2021

    @colinl
    Contributor

    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.

  15. kuema commented on Mar 3, 2021

    @kuema

    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.

  16. 5 remaining items

  17. normanr commented on Nov 7, 2021

    @normanr

    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.

  18. dceejay commented on Nov 7, 2021

    @dceejay
    Member

    Status is still your friend as that hasn’t changed

  19. shurkanTwo commented on Jun 9, 2022

    @shurkanTwo

    Does anybody know when we can expect the 5th task of the ticket to be done? :)

  20. Steve-Mcl commented on Jun 9, 2022

    @Steve-Mcl
    Contributor

    @shurkanTwo

    5th task of the ticket to be done

    To what are you refering?

  21. shurkanTwo commented on Jun 9, 2022

    @shurkanTwo

    @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?

  22. dceejay commented on Jun 9, 2022

    @dceejay
    Member

    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.
    image

  23. shurkanTwo commented on Jun 9, 2022

    @shurkanTwo

    @dceejay ah okay I see, thank's a lot! :)
    So there has not been any progress so far?

  24. JsBergbau commented on Jun 10, 2023

    @JsBergbau
    Author

    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)

    node.publish = function (msg,done) {
                if (node.connected || (msg.queueMsg && msg.qos > 0) {
                    if (msg.payload === null || msg.payload === undefined) {

    That would be a very efficient fix without breaking the current behaviour.

  25. m4rc0d1 commented on Dec 13, 2023

    @m4rc0d1

    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.

  26. NorbNorb commented on Apr 23, 2024

    @NorbNorb

    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.

  27. colinl commented on Apr 23, 2024

    @colinl
    Contributor

    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.

  28. hlovdal commented on Dec 18, 2024

    @hlovdal
    Contributor

    Steve Cope has a video Node-Red -MQTT Message Buffer (flow download) where he shows how to do buffering using function nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions