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

**URL:** https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309
**Category:** General
**Tags:** mqtt
**Created:** [28 March 2025 10:56 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309 "2025-03-28T10:56:50Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [28 March 2025 10:56 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/1 "2025-03-28T10:56:50Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 March 2025 11:06 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/2 "2025-03-28T11:06:51Z")

</div>

> [@greengolfer](#):
>
> 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](https://mntolia.com/10-free-public-private-mqtt-brokers-for-testing-prototyping/) (emqx and mosquitto work well) and I will check it out.

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [28 March 2025 12:01 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/3 "2025-03-28T12:01:39Z")

</div>

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

```json
[{"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":""}]

```

 ![Screenshot 2025-03-28 at 12.58.42](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/997516bb7a87400e4a5851ced39eae898ca85c77.png)

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 March 2025 13:15 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/4 "2025-03-28T13:15:32Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4461c2cbfbf4749be91c304e5b1a6476f3a6717.gif)

```auto
[{"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":""}]

```

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [28 March 2025 13:36 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/5 "2025-03-28T13:36:59Z")

</div>

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

 ![Screenshot 2025-03-28 at 14.35.20](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73e77ca0b95b09df8a99d72962407ddb5552a8cd.png)

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 March 2025 13:58 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/6 "2025-03-28T13:58:02Z")

</div>

> [@greengolfer](#):
>
> And the flow you shared is not identical at the image.

Updated - try now

> [@greengolfer](#):
>
> I have reused my example and add :8883 in the msg.broker.url. No difference. Still "yellow" every other click.

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 March 2025 14:48 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/7 "2025-03-28T14:48:35Z")

</div>

Issue raised: [Operating MQTT connect action twice results in endless "connecting" state · Issue #5095 · node-red/node-red · GitHub](https://github.com/node-red/node-red/issues/5095)

---

<div class="post-metadata">

### Author: ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)
#### Post date: [28 March 2025 16:05 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/8 "2025-03-28T16:05:28Z")

</div>

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` 🙂

Thank you for your continuous support.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [11 April 2025 16:06 UTC](https://discourse.nodered.org/t/with-msg-action-connect-sent-twice-stuck-the-mqtt-node-in-connecting/96309/9 "2025-04-11T16:06:05Z")

</div>

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