# Stopping node when removed

**URL:** <https://discourse.nodered.org/t/stopping-node-when-removed/42916>\
**Category:** General\
**Created:** [20 March 2021 08:03 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916 "2021-03-20T08:03:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [20 March 2021 08:03 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/1 "2021-03-20T08:03:52Z")

</div>

I am writing a node to monitor a socket with a custom protocol. It works fine and I had to setup a setInterval to catch random disconnections and re-connect, triggered by the 'close' event.

Now, if I remove the node from my workspace and deploy changes, I see that the node is still working in the background, trying to connect to the server. In which function I can stop the timer when the node is removed from the workspace?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 March 2021 08:55 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/2 "2021-03-20T08:55:27Z")

</div>

Covered in the docs here: [JavaScript file : Node-RED](https://nodered.org/docs/creating-nodes/node-js#closing-the-node)

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [20 March 2021 10:03 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/3 "2021-03-20T10:03:05Z")

</div>

Hi Nick,  
yes I am working with that doc. My problem is that when I receive a "close" (`this.on('close')`) I need to keep the timer running to retry a connection. I I stop it in that function, the node will definitely close at first error.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 March 2021 10:23 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/4 "2021-03-20T10:23:32Z")

</div>

If you receive a close event on the node, that means the node is being stopped and you must tidy up all of its resources. You absolutely should not be reconnecting to a server in response.

Are you mixing up the close event of the node wit the close event of whatever connection you have created?

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [20 March 2021 10:48 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/5 "2021-03-20T10:48:22Z")

</div>

I am clearing the resource before trying to re-instantiate:

```auto

      node.client.on('close', function(removed, done) {
        if (removed) {
          node.error('IP connection closed')
         node.client.destroy()
        } else {
          instantiateClient()
        }
      })

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 March 2021 10:58 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/6 "2021-03-20T10:58:20Z")

</div>

What exactly is `node.client`?

As I thought, it looks like you are confusing the close event of your network client with the close event of the node.

The `removed` argument only exists for the close event of the node. But your code looks like to you are trying to handle when your network client loses its connection - which is an entirely different close event.

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [20 March 2021 11:00 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/7 "2021-03-20T11:00:10Z")

</div>

Yes, I am sure I am mixing up things...

```auto
node.client = new net.Socket()

```

it's a socket.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 March 2021 11:11 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/8 "2021-03-20T11:11:30Z")

</div>

So, to answer your original question, you need to register a handler for the close event on the `node` object itself.

In that handler you can close any connections and clear any timers you've created.

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [20 March 2021 11:26 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/9 "2021-03-20T11:26:55Z")

</div>

Thanks for your patience!

---

<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:** [3 April 2021 11:27 UTC](https://discourse.nodered.org/t/stopping-node-when-removed/42916/10 "2021-04-03T11:27:45Z")

</div>

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