MQTT 'Connection failed to broker'

Hi there,
I am building a vertical farm sensing and control system.
The sensor data are gathered into my node-red server through MQTT server (Mosquitto).

Recently, console alert 'connection failed to broker' messages pops up frequently.
And MQTT communication published from sensor fails to connect,
however, publishing to sensor from server seems fine.

Why this problem has occured?
I attached some error message below. Please give some advice!
image

How many MQTT Node Connections are possible?
I guess the problem is because of MQTT Connection limit...
System has a lot of connection depended one MQTT node-server.
Does this cause the problem?

If you open any one of your MQTT in nodes then select the broker dropdown selection list. How many broken config nodes do you have??

I have only one broker.
image

And I'm subscribed to about 50 topics on that broker.

ok, thats good.

How many MQTT clients apart from node-red do you have (i.e. ESPs, other MQTT devices) reading/writing to the actual broker?

How frequent are messages? (1 per sec? 100 per sec, 10000 per sec)? Just an estimate is fine.

Has the broker simply crashed? Does the connection recover after you restart the MQTT broker?

PS, what broker are you using and what version is it?

Thank you for your kind reply.

To answer your question:

  1. There are about 100 mqtt clients.
    And on our system we have nodered processes running on different ports. (like one is 'node-red -p 1000', the other is 'node-red -p 1001') Another process also has about 15+ clients.

  2. The process where the problem occurs is about 100+ every 2 seconds. There may be more to come because we don't know for sure.

  3. Most of them remain broken. It seems to reconnect very, very rarely.
    Also, if you create a new MQTT Node, that node also works well.
    It seems that the ones that don't work well are the nodes I've already created.

Finally, I use moquitto (v3.1.1) as my broker.

ps. Occurs primarily on MQTT nodes located in subflows.

Thank you very much.

Ah now that makes more sense with what your terminal screen dump shows.

How many subflows (a rough estimate) on this problematic Node-RED instance?

We have about 30 subflows.
In subflow, it is responsible for parsing and processing data from MQTT.

could you export one of your subflows please?

To do this

  • select 1 subflow instance on a regular flow
  • press CTRL-E
  • Start a reply on the forum
  • click the code button </> in the forum toolbar
  • paste the exported JSON

Inevitably, the server url was deleted.

[
    {
        "id": "7fe048f505bd8db7",
        "type": "subflow",
        "name": "setCurTemp",
        "info": "",
        "category": "",
        "in": [],
        "out": [
            {
                "x": 980,
                "y": 100,
                "wires": [
                    {
                        "id": "dc479e888e57b1b3",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1000,
                "y": 140,
                "wires": [
                    {
                        "id": "5c3172c721d90656",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1370,
                "y": 280,
                "wires": [
                    {
                        "id": "3825b36d60f9a406",
                        "port": 0
                    },
                    {
                        "id": "d62a7614f23834f6",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1360,
                "y": 360,
                "wires": [
                    {
                        "id": "f5f3e42d7fdbb76f",
                        "port": 0
                    },
                    {
                        "id": "27b4409555559201",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1360,
                "y": 480,
                "wires": [
                    {
                        "id": "f13490c1f51eb4e9",
                        "port": 0
                    },
                    {
                        "id": "bae6c4d3c0aed585",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1320,
                "y": 560,
                "wires": [
                    {
                        "id": "e564c003e5bf429b",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1190,
                "y": 640,
                "wires": [
                    {
                        "id": "349cc561ea512842",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "SECTOR",
                "type": "str",
                "value": "oc/"
            }
        ],
        "meta": {},
        "color": "#3FADB5",
        "outputLabels": [
            "setTemp",
            "setCurrentTemp",
            "setDefrostingWay",
            "setVentilateCtrlDefro",
            "setVentilateCtrlFrozen",
            "setSystemCondition",
            "setErrorConditoin"
        ]
    },
    {
        "id": "e50167dbfa994cf0",
        "type": "switch",
        "z": "7fe048f505bd8db7",
        "name": "switch",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "hask",
                "v": "setTemp",
                "vt": "str"
            },
            {
                "t": "hask",
                "v": "setCurrentTemp",
                "vt": "str"
            },
            {
                "t": "hask",
                "v": "setDefrostingWay",
                "vt": "str"
            },
            {
                "t": "hask",
                "v": "setVentilateCtrlDefro",
                "vt": "str"
            },
            {
                "t": "hask",
                "v": "setVentilateCtrlFrozen",
                "vt": "str"
            },
            {
                "t": "hask",
                "v": "setSystemCondition",
                "vt": "str"
            },
            {
                "t": "hask",
                "v": "setErrorConditoin",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 7,
        "x": 490,
        "y": 300,
        "wires": [
            [
                "dc479e888e57b1b3"
            ],
            [
                "5c3172c721d90656"
            ],
            [
                "e33acc35d8534b59"
            ],
            [
                "8bec506697faebc0"
            ],
            [
                "f3613415739f3849"
            ],
            [
                "70791a51d7dffa32"
            ],
            [
                "349cc561ea512842"
            ]
        ],
        "outputLabels": [
            "temp",
            "",
            "",
            "",
            "",
            "",
            ""
        ]
    },
    {
        "id": "4c9cf01d71ef2434",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.value",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "slaveNum",
                "pt": "msg",
                "to": "payload.slaveNum",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 260,
        "y": 300,
        "wires": [
            [
                "e50167dbfa994cf0"
            ]
        ]
    },
    {
        "id": "dc479e888e57b1b3",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"value\": payload.setTemp}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 760,
        "y": 100,
        "wires": [
            []
        ]
    },
    {
        "id": "5c3172c721d90656",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"value\": payload.setCurrentTemp}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 760,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "e33acc35d8534b59",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"value\": payload.setDefrostingWay}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 280,
        "wires": [
            [
                "9e11f788737b6f6a"
            ]
        ]
    },
    {
        "id": "8bec506697faebc0",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"value\": payload.setVentilateCtrlDefro}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 340,
        "wires": [
            [
                "69329fd4c611a2f2"
            ]
        ]
    },
    {
        "id": "f3613415739f3849",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"value\": payload.setVentilateCtrlFrozen}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 720,
        "y": 480,
        "wires": [
            [
                "3df537af39f2ac6c"
            ]
        ]
    },
    {
        "id": "70791a51d7dffa32",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"value\": payload.setSystemCondition}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 720,
        "y": 560,
        "wires": [
            [
                "e564c003e5bf429b"
            ]
        ]
    },
    {
        "id": "349cc561ea512842",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"value\": payload.setErrorConditoin}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 720,
        "y": 640,
        "wires": [
            []
        ]
    },
    {
        "id": "9e11f788737b6f6a",
        "type": "switch",
        "z": "7fe048f505bd8db7",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 870,
        "y": 280,
        "wires": [
            [
                "3825b36d60f9a406"
            ],
            [
                "d62a7614f23834f6"
            ]
        ]
    },
    {
        "id": "3825b36d60f9a406",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "d62a7614f23834f6",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 300,
        "wires": [
            []
        ]
    },
    {
        "id": "69329fd4c611a2f2",
        "type": "switch",
        "z": "7fe048f505bd8db7",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 870,
        "y": 340,
        "wires": [
            [
                "f5f3e42d7fdbb76f"
            ],
            [
                "27b4409555559201"
            ]
        ]
    },
    {
        "id": "f5f3e42d7fdbb76f",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "27b4409555559201",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 400,
        "wires": [
            []
        ]
    },
    {
        "id": "3df537af39f2ac6c",
        "type": "switch",
        "z": "7fe048f505bd8db7",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 890,
        "y": 480,
        "wires": [
            [
                "f13490c1f51eb4e9"
            ],
            [
                "bae6c4d3c0aed585"
            ]
        ]
    },
    {
        "id": "f13490c1f51eb4e9",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1070,
        "y": 460,
        "wires": [
            []
        ]
    },
    {
        "id": "bae6c4d3c0aed585",
        "type": "change",
        "z": "7fe048f505bd8db7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 500,
        "wires": [
            []
        ]
    },
    {
        "id": "e564c003e5bf429b",
        "type": "function",
        "z": "7fe048f505bd8db7",
        "name": "",
        "func": "// error  masking table \nconst status_mask = [\"power\", \"frozen\", \"solenoid\", \"compresher\",\n                     \"fan\", \"frozenHeater\", \"roomHeater\"\n                    ];\n\nconst value = msg.payload.value;\nlet mask = 1;\n\n\nfor (let status of status_mask) {\n    if ((value & mask) && status !== null) {\n        msg.payload[status] = \"ON\";\n    }\n    msg.payload[status] = \"OFF\"\n    mask = mask<<1;\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1000,
        "y": 560,
        "wires": [
            []
        ]
    },
    {
        "id": "9adfcda93e8526d7",
        "type": "mqtt in",
        "z": "7fe048f505bd8db7",
        "name": "",
        "topic": "",
        "qos": "2",
        "datatype": "auto",
        "broker": "",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 50,
        "y": 300,
        "wires": [
            [
                "4c9cf01d71ef2434"
            ]
        ]
    }
]

That was the interesting part.

I suspect you have scoped the MQTT-Broker "connection" to the subflow.

There are 2 ways to improve this (I strongly recommend FIX #1)

FIX #1

Take the MQTT IN node OUT of the subflow & feed it into the subflow from a main flow.
This will make sure only 1 connection to the broker is made and reduce the stress on the broker.
PS: make sure the connection is scoped to global.

FIX #2

Leave the MQTT IN node inside the subflow and change its scope to "global".
This way, only one connection is created.


PS, what version mosquitto is being used (v3.1.1 is the MQTT protocol version not the binary version of mosquitto)

Thanks for the good solution. I'll try what you said.
(Probably time consuming.)

Hello Steve. :grinning: :grinning: :grinning:
I'm on a business trip, so I'm leaving a reply now.
Thanks to the advice you gave me last time, MQTT communication works fine.
Tried Fix1, MQTT communication recovered.
But unfortunately it still results in MQTT CONNECTION FAILED.

As a result of testing for about a day, continuous connection and communication are possible.
But is there any way to get rid of that error?

Show us the errors you are seeing in the log please. Don't screenshot, copy/paste the text here.

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