# Force disconnection of TCP connection

**URL:** <https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436>\
**Category:** General\
**Created:** [29 October 2023 07:40 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436 "2023-10-29T07:40:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [29 October 2023 07:40 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/1 "2023-10-29T07:40:21Z")

</div>

In my fun MQTT broker project, part of the MQTT spec is to force a disconnection to a connected client from my broker under certain circumstances.

I don't think it looks possible to do that with the TCP nodes but just asking in case it is

---

<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:** [29 October 2023 08:33 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/2 "2023-10-29T08:33:02Z")

</div>

I don't think it is. Certainly not in the sidebar documentation.

Pretty sure I've seen this asked before, more than once.

I'm ok with this being raised as a feature request but I'd be happier if @dceejay backed it up (he has more background on the TCP nodes than I do)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [29 October 2023 08:35 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/3 "2023-10-29T08:35:57Z")

</div>

I'm not too bothered if it doesn't do it, as its just a play project, so I won't pursue it as an FR 🙂

---

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [30 October 2023 09:31 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/4 "2023-10-30T09:31:41Z")

</div>

Yes, I also asked this question about how to manually (via message) disconnect / reconnect a TCP connection because it happens that a connection gets unexpected disconnected after a while (at least in my setup via VPN).

A manual disconnection in my opinion _is_ necessary if you connect to a server with the option "never disconnect". You should be able to close this connection and select a different server, right?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 October 2023 18:29 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/5 "2023-10-30T18:29:41Z")

</div>

> [@FloRu](#):
>
> You should be able to close this connection and select a different server,

And interesting definition of "never disconnect"....

---

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [30 October 2023 18:54 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/6 "2023-10-30T18:54:19Z")

</div>

@dceejay  
Yes, you're right, that's interesting... 🤔 - I think I should have read my post again, before posting this... 😉  
But I think you know, what I mean?

I mean, it would be good to have the option to close the connection manually. Not automatically. But when you need to. In this case it's the choice of the user of the NR instance. If he decides to connect to a different server, he should be able to do so, and close the foreign connection therefore...

---

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [2 November 2023 18:52 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/7 "2023-11-02T18:52:31Z")

</div>

@dceejay  
What do you think about this addon?  
Or is there any other solution for getting a TCP Connection opened, and kept open with the chance to reset / reconnect if the connection gets dropped?  
In my setup with the VPN connection a disconnected session doesn't get reconnected when I just resend the connection message...

Thanks!  
Florian

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 November 2023 19:37 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/8 "2023-11-02T19:37:30Z")

</div>

Pull request raised here [Let msg.reset reset Tcp request node connection when in stay connected mode by dceejay · Pull Request #4406 · node-red/node-red · GitHub](https://github.com/node-red/node-red/pull/4406)

---

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [2 November 2023 19:55 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/9 "2023-11-02T19:55:29Z")

</div>

Great! Thanks for raising!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [3 November 2023 09:38 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/10 "2023-11-03T09:38:41Z")

</div>

Will this PR take account of the sesssion id property and only reset that connection?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 November 2023 10:17 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/11 "2023-11-03T10:17:50Z")

</div>

This is the request node - so it is connecting out to only one place - so yes.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [3 November 2023 10:18 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/12 "2023-11-03T10:18:27Z")

</div>

Excellent then 🙂

---

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [3 November 2023 10:20 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/13 "2023-11-03T10:20:13Z")

</div>

@dceejay  
This is interesting.  
If using the same server with different ports you can open up two (or maybe more) connections at a time. Is this meant to be or is this just coincidence?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 November 2023 10:26 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/14 "2023-11-03T10:26:56Z")

</div>

true. need to check. I suspect in that case it will reset the last connection.

---

<div class="post-metadata">

**Author:** ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)\
**Post date:** [3 November 2023 10:29 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/15 "2023-11-03T10:29:30Z")

</div>

Would be good to have one of those options:

- reset/drop all previous connections
- reset/drop a defined connection (when sending the desired port)

I think, I would prefer the first option.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 November 2023 10:29 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/16 "2023-11-03T10:29:54Z")

</div>

I wonder if its cleaner, to set `reset` to an id?  
and pass this id on the output - so one can 'store' the ids - that they can then disconnect from so to speak?

(I'm just thinking out aloud)

**EDIT**  
But then again - it only supports one active connection, if I am not mistaken

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 November 2023 10:35 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/17 "2023-11-03T10:35:47Z")

</div>

Looking at it some more.. it stores connections by a key of `host:port` so the sender already knows that info... so... we could...  
if msg.reset set and no msg.host and/or msg.port set then close all.  
if msg.reset and msg.host and msg.port then close that specific connection.

thoughts ?

EDIT: - this does mean that if you supply reset host and port then it won't close an old connection and restart a new one. it will just reset an existing one. (current fix behaviour is to close the last one and start a new one)

so yes maybe accepting msg.reset = host:port would let us target specific one.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 November 2023 10:44 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/18 "2023-11-03T10:44:06Z")

</div>

\<Can of worms open\>

Sorry - I got dibs on brain activity currently

```auto
reset:'last' /* Last Connected */
reset:'all' /* All connection */

```

Examples:

```auto
/* Clears all - no new */
reset:'all'

```

```auto
/* Clears last - as well as creating a new one */
reset:'last',
port:4050,
host:'x.x.x.x'

```

Both off these can include the new host detail, and no matter what happens with reset, a connection with the new details will be made (if provided)

This would allow to purge all connections, without having to send 2 messages, to both clear all and start a new connection.

**EDIT**  
I think you just addressed my suggestion with your edit 😆

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [3 November 2023 10:49 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/19 "2023-11-03T10:49:22Z")

</div>

> [@marcus-j-davies](#):
>
> `/* Clears all - no new */`

that can never happen - you are in stay connected mode - it will try to reconnect to something - either the host:port specified in the config - or the one supplied... if not supplied and not in config it's an error.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [3 November 2023 11:00 UTC](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436/20 "2023-11-03T11:00:24Z")

</div>

> [@dceejay](#):
>
> if msg.reset and msg.host and msg.port then close that specific connection.

Well I'm just using session.id to direct messages at the moment

So, I'm taking in message with one session.id and then either replying to them using same id or changing it to another connections id to pass it on

I'm not storing address or port and reply node seems happy to just rely on session.id

So, I'd like to send msg.reset and msg.\_session.id and have the node work out the address:port

[Next page](https://discourse.nodered.org/t/force-disconnection-of-tcp-connection/82436.md?page=2)
