# How to make my custom node disconnect cleanly

**URL:** <https://discourse.nodered.org/t/how-to-make-my-custom-node-disconnect-cleanly/47406>\
**Category:** Developing Nodes\
**Created:** [20 June 2021 17:57 UTC](https://discourse.nodered.org/t/how-to-make-my-custom-node-disconnect-cleanly/47406 "2021-06-20T17:57:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdkrieg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdkrieg/32/39091_2.png) [@mdkrieg](https://discourse.nodered.org/u/mdkrieg)\
**Post date:** [20 June 2021 17:57 UTC](https://discourse.nodered.org/t/how-to-make-my-custom-node-disconnect-cleanly/47406/1 "2021-06-20T17:57:44Z")

</div>

Hello,

I'm working on a custom node that connects to an external websocket. My problem right now is that after I restart node-red the external API gives me a "connection limit exceeded" message.

I think there's a few ways I need to change my code but one I need help with is how can I detect when node-red is closing so that I can make a disconnect() call?

Appreciate the help, thanks,  
-Matt

---

<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 June 2021 18:00 UTC](https://discourse.nodered.org/t/how-to-make-my-custom-node-disconnect-cleanly/47406/2 "2021-06-20T18:00:38Z")

</div>

Hi @mdkrieg

you need to handle the 'close' event - this is triggered whenever the node is being stopped. That can be due to a new flow configuration being deployed, or the runtime stopping.

[https://nodered.org/docs/creating-nodes/node-js#closing-the-node](https://nodered.org/docs/creating-nodes/node-js#closing-the-node)

---

<div class="post-metadata">

**Author:** ![mdkrieg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdkrieg/32/39091_2.png) [@mdkrieg](https://discourse.nodered.org/u/mdkrieg)\
**Post date:** [20 June 2021 18:10 UTC](https://discourse.nodered.org/t/how-to-make-my-custom-node-disconnect-cleanly/47406/3 "2021-06-20T18:10:30Z")

</div>

Perfect, of course it would be something simple. Thanks!

---

<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:** [4 July 2021 18:11 UTC](https://discourse.nodered.org/t/how-to-make-my-custom-node-disconnect-cleanly/47406/4 "2021-07-04T18:11:17Z")

</div>

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