# Listen to events only when node is in use

**URL:** https://discourse.nodered.org/t/listen-to-events-only-when-node-is-in-use/67970
**Category:** Developing Nodes
**Created:** [20 September 2022 12:10 UTC](https://discourse.nodered.org/t/listen-to-events-only-when-node-is-in-use/67970 "2022-09-20T12:10:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![steinmetz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steinmetz/32/57992_2.png) [@steinmetz](https://discourse.nodered.org/u/steinmetz)
#### Post date: [20 September 2022 12:10 UTC](https://discourse.nodered.org/t/listen-to-events-only-when-node-is-in-use/67970/1 "2022-09-20T12:10:22Z")

</div>

Hi all!

Is there a way to listen to editor events such as `deploy` only if my node is in use? I read this page but I couldn't find a function I could override when my node is dragged from the palette to the workspace ([HTML File : Node-RED](https://nodered.org/docs/creating-nodes/node-html)).

Thank you for your help!

---

<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: [20 September 2022 13:19 UTC](https://discourse.nodered.org/t/listen-to-events-only-when-node-is-in-use/67970/2 "2022-09-20T13:19:00Z")

</div>

You will typically do this in your JS Module file.

```auto
module.exports = function (RED) {

    function Init(config) {
        RED.events.on("deploy", HandleDeploy);
    }

    function HandleDeploy(){
       // Do something
    }

    RED.nodes.registerType('MyModule', Init);
}

```

The above will come into contact when its already in a users flow  
remember to destroy the listener if your node is restarted or removed from the flow 😅

```auto
this.on('close', (removed, done) => {
  RED.events.off("deploy", HandleDeploy);
  done();
})

```

[RED.events : Node-RED (nodered.org)](https://nodered.org/docs/api/ui/events/)

---

<div class="post-metadata">

### Author: ![steinmetz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steinmetz/32/57992_2.png) [@steinmetz](https://discourse.nodered.org/u/steinmetz)
#### Post date: [20 September 2022 19:39 UTC](https://discourse.nodered.org/t/listen-to-events-only-when-node-is-in-use/67970/3 "2022-09-20T19:39:53Z")

</div>

Hi! Thank you for your reply!  
Unfortunately, I can't make it work in the JS Module file... it seems that editor events (such as deploy) can only be called from the HTML file (I am not sure tho).

My code is here, do you see any problem with it?

> <https://github.com/steinmetz/node-red-contrib-digital-twin/blob/dev/src/dt-event/dt-event.js>

thanks again!

---

<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: [19 November 2022 19:40 UTC](https://discourse.nodered.org/t/listen-to-events-only-when-node-is-in-use/67970/4 "2022-11-19T19:40:42Z")

</div>

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