With msg.action=connect sent twice stuck the MQTT node in "connecting"

I am using a lot the dynamic configuration of MQTT nodes.
Using msg.broker and msg.action, I can configure the remote broker and connect to it.
I noticed that when msg.action=connect is sent twice (I know it shouldn't be done, but, things happen...), then the MQTT node is stuck, forever, in connecting mode.
If the msg.action=connect is sent again then the connection is reestablished and status is green.
In short, an odd number of connect works, an even number of connect stuck the MQTT node in connecting (yellow status).
Is that the expected behaviour ?

No, but it depends on what else you are sending (or not sending).
Also, it might be a "status" issue (i.e. is it really connecting (or is it just saying that))

Please provide a repro demo flow using inject nodes to connect to public MQTT brokers (emqx and mosquitto work well) and I will check it out.

Hi,
Here is a basic test flow, reproducing the same behaviour using broker.emqx.io

[{"id":"1119d662c3c0c954","type":"change","z":"5480202f19632c6c","name":"Handle connection","rules":[{"t":"set","p":"action","pt":"msg","to":"connect","tot":"str"},{"t":"set","p":"broker","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"broker.force","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"broker.url","pt":"msg","to":"mqtts://broker.emqx.io","tot":"str"},{"t":"set","p":"broker.usetls","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"broker.protocolVersion","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":220,"wires":[["adf1cf83caa3f08d"]]},{"id":"adf1cf83caa3f08d","type":"mqtt out","z":"5480202f19632c6c","name":"Subscriber","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"20f13f9c318f30d1","x":690,"y":220,"wires":[]},{"id":"c9abb69de0593fa8","type":"inject","z":"5480202f19632c6c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":220,"wires":[["1119d662c3c0c954"]]},{"id":"9c018a15ec7e776d","type":"mqtt in","z":"5480202f19632c6c","name":"","topic":"this_is_a_test_topic","qos":"1","datatype":"auto-detect","broker":"20f13f9c318f30d1","nl":false,"rap":true,"rh":0,"inputs":0,"x":490,"y":340,"wires":[["c0be24c5be0695c3"]]},{"id":"c0be24c5be0695c3","type":"debug","z":"5480202f19632c6c","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":340,"wires":[]},{"id":"20f13f9c318f30d1","type":"mqtt-broker","name":"EMQX","broker":"\"\"","port":"1883","clientid":"","autoConnect":false,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

And when it is "yellow", nothing is received.
I have used MQTT Explorer from my laptop to publish/subscribe to/from the broker.

So there is some quirk with your method, but it should work TBH.

I suspect because you have set 1883 in the MQTT config BUT are specifying a broker URL without PORT in the broker object, it is somehow using port 1883 instead of inferring port 8883 from the secure URL in broker.url

The solution is to include broker.port and use broker.broker (though broker.url: mqtts://address:8883 might work too)

Working demo...

chrome_LshPiCjGVd

[{"id":"1119d662c3c0c954","type":"change","z":"4cd8746ea8d2c450","name":"Connect","rules":[{"t":"set","p":"action","pt":"msg","to":"connect","tot":"str"},{"t":"set","p":"broker.broker","pt":"msg","to":"broker.emqx.io","tot":"str"},{"t":"set","p":"broker.port","pt":"msg","to":"8883","tot":"num"},{"t":"set","p":"broker.usetls","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"broker.protocolVersion","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1340,"y":240,"wires":[["adf1cf83caa3f08d"]]},{"id":"adf1cf83caa3f08d","type":"mqtt out","z":"4cd8746ea8d2c450","name":"Subscriber","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"20f13f9c318f30d1","x":1630,"y":340,"wires":[]},{"id":"c9abb69de0593fa8","type":"inject","z":"4cd8746ea8d2c450","name":"connect with force","props":[{"p":"payload"},{"p":"broker.force","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1110,"y":240,"wires":[["1119d662c3c0c954"]]},{"id":"9c018a15ec7e776d","type":"mqtt in","z":"4cd8746ea8d2c450","name":"","topic":"nr-test-1/time","qos":"1","datatype":"auto-detect","broker":"20f13f9c318f30d1","nl":false,"rap":true,"rh":0,"inputs":0,"x":1430,"y":440,"wires":[["c0be24c5be0695c3"]]},{"id":"c0be24c5be0695c3","type":"debug","z":"4cd8746ea8d2c450","name":"time","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1650,"y":440,"wires":[]},{"id":"18b941ecd5c0e44c","type":"mqtt out","z":"4cd8746ea8d2c450","name":"","topic":"nr-test-1/time","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"20f13f9c318f30d1","x":1270,"y":440,"wires":[]},{"id":"33c1f912ad70c6c4","type":"inject","z":"4cd8746ea8d2c450","name":"time","props":[{"p":"payload"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$moment().format('HH:mm:ss')","payloadType":"jsonata","x":1070,"y":440,"wires":[["18b941ecd5c0e44c"]]},{"id":"aa7ae0d65e3ba38e","type":"inject","z":"4cd8746ea8d2c450","name":"disconect/pause/connect","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1130,"y":180,"wires":[["e098673fcb7de9da"]]},{"id":"ebe6ec46d68992a3","type":"delay","z":"4cd8746ea8d2c450","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1525,"y":180,"wires":[["1119d662c3c0c954"]],"l":false},{"id":"d5723106f9f210e6","type":"inject","z":"4cd8746ea8d2c450","name":"disconnect action  .","props":[{"p":"action","v":"disconnect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1110,"y":340,"wires":[["adf1cf83caa3f08d"]]},{"id":"d647b911c171543b","type":"inject","z":"4cd8746ea8d2c450","name":"connect no force","props":[{"p":"payload"},{"p":"broker.force","v":"false","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1100,"y":300,"wires":[["1119d662c3c0c954"]]},{"id":"e098673fcb7de9da","type":"change","z":"4cd8746ea8d2c450","name":"action disconnect","rules":[{"t":"set","p":"action","pt":"msg","to":"disconnect","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1370,"y":180,"wires":[["adf1cf83caa3f08d","ebe6ec46d68992a3"]]},{"id":"20f13f9c318f30d1","type":"mqtt-broker","name":"EMQX","broker":"\"\"","port":"1883","clientid":"","autoConnect":false,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

I have reused my example and add :8883 in the msg.broker.url. No difference. Still "yellow" every other click.

And the flow you shared is not identical at the image.

Updated - try now

Yeah, I tried that too. Definitely a quirk. Luckily setting broker and port do work.

Issue raised: Operating MQTT connect action twice results in endless "connecting" state · Issue #5095 · node-red/node-red · GitHub

Thank you for raising this issue.

I have tried different things.

  1. msg.broker.url = mqtts://broker.emqx.io with or without additional port has the issue described above
  2. msg.broker.broker = mqtts://broker.emqx.io and msg.broker.port = 1883 or 8883 or... has no problem. Multiple attempt to connect ends up with the connection established. No "yellow".
  3. Surprisingly enough, msg.broker.broker = mqtts://broker.emqx.io:8883 (so exact same syntax as url) works too. No need for msg.broker.port

So, I guess I'll use the msg.broker.broker :slight_smile:

Thank you for your continuous support.