# How to respond to deployments in a plugin

**URL:** <https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267>\
**Category:** Developing Nodes\
**Created:** [6 May 2022 07:01 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267 "2022-05-06T07:01:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 May 2022 07:01 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/1 "2022-05-06T07:01:10Z")

</div>

Hi folks,

I am trying to use plugins to create a sidebar, based on this [gist](https://gist.github.com/knolleary/2b5cf9f79a435180f6f7295e3967d9b9) example.  
It seems that the `onadd` is only called at the beginning.  
But I need to do some re-initialization after every deploy (where I can search for the newly deployed nodes).

Unfortunately my Node-RED development time has dropped below freezing point, so I don't have time to start digging into the core.

Can anybody give me a tip about how to handle deployments in a plugin.

Thanks!!!!  
Bart

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 May 2022 08:15 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/2 "2022-05-06T08:15:51Z")

</div>

You have 3 events that you can add listeners to `'nodes:add'`, `'nodes:change'`, `'nodes:remove'`.

I use them in combination with an API to detect committed changes vs uncommitted. That's because uibuilder needs to track url names regardless of whether deployed or not.

```auto
    RED.events.on('nodes:add', function(node) {
        if ( node.type === 'uibuilder') {
            // Keep a list of uib nodes in the editor
            // may be different to the deployed list
            editorInstances[node.id] = node.url
            // -- IF uibuilderInstances <> editorInstances THEN there are undeployed instances. --
        } 
    })
    RED.events.on('nodes:change', function(node) {
        if ( node.type === 'uibuilder') {
            mylog('nodes:change:', node)
            editorInstances[node.id] = node.url
        }
    }) 
    RED.events.on('nodes:remove', function(node) {
        if ( node.type === 'uibuilder') {
            mylog('>> nodes:remove >>', node)
            delete editorInstances[node.id]
        }
    })

```

---

<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:** [6 May 2022 08:28 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/3 "2022-05-06T08:28:08Z")

</div>

The full list of editor events are listed here:

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

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 May 2022 17:13 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/4 "2022-05-06T17:13:41Z")

</div>

Hi guys,

I think my question was not complete enough. I meant on the _ **server side** _ of the plugin.

My .js file contains this:

```auto
RED.plugins.registerPlugin("my-sidebar-plugin", {
   onadd: function() {
      // The server side code of my plugin
   }
})

```

The `onadd` is nicely called at the startup, so I can use that to initialize some stuff.  
But I was wondering if such a plugin also offered things like `ondeploy`, `onclose`, ...

Because I need to execute some code at the server side of my plugin, after a deploy has been executed.

I am aware that my plugin has no data from its own, but I need to collect on the server some information about data of nodes that have been deployed.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 May 2022 17:19 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/5 "2022-05-06T17:19:48Z")

</div>

> [@BartButenaers](#):
>
> onclose

Yes,

```auto
this.on('close', (removed,done) => {
    // ...
})

```

from inside the registered callback function.

> [@BartButenaers](#):
>
> ondeploy

I don't think you need that since your registered callback will be executed if a node instance is (re)deployed.

So this is how uibuilder is structured:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5d436fba5bde832afdfe4cb52620d4ee9f000546.png)

The on close handler is defined in `nodeInstance` which also creates the node and registers the on input callback.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 May 2022 17:22 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/6 "2022-05-06T17:22:40Z")

</div>

> [@TotallyInformation](#):
>
> your registered callback will be executed if a node instance is (re)deployed.

Julian,  
the `onadd` callback function of my plugin is only called once at the start, but not at a (re)deploy unfortunately.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [6 May 2022 17:26 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/7 "2022-05-06T17:26:10Z")

</div>

Will get back here tomorrow. My Node-RED time is up again for today...

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 May 2022 17:26 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/8 "2022-05-06T17:26:18Z")

</div>

I might be confused but:

- `onadd` is only run on FIRST deploy
- `nodeInstance` is run when any node instance is deployed or redeployed
- `runtimeSetup` is only run once - I think when the Node is added to the palette
- on close is run whenever a node instance is removed and deploy or when Node-RED closes, it runs for each instance.

What do you actually want to run and when?

Sorry - I think I AM confused because you are deploying a PLUGIN not a node 🙂

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [7 May 2022 07:00 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/9 "2022-05-07T07:00:04Z")

</div>

Hi Julian,  
Nice example in your screenshot!! Very self explaining...

Yes indeed that all works fine for a **NODE** , but I don't know if that is also possible for a _ **PLUGIN** _.  
I need something similar to your nodeInstance, but for a plugin.  
Not sure how I can achieve that.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 May 2022 10:00 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/10 "2022-05-07T10:00:31Z")

</div>

Yep. I've not done a plugin and I was a bit slow on the uptake 🙂

---

<div class="post-metadata">

**Author:** ![tve](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tve/32/59475_2.png) [@tve](https://discourse.nodered.org/u/tve)\
**Post date:** [9 May 2022 04:02 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/11 "2022-05-09T04:02:32Z")

</div>

@BartButenaers There are 4 events of interest (I may not get all the names right): flows:stopping, flows:stopped, flows:starting, and flows:started. I settled to using the flows:started one. You get passed the event type, a diff of nodes (to be removed, to be changed, to be started, something like that...), and the full flow config. The config tells you about all nodes coming from the flow editor, i.e., these are not exactly the running nodes (subflows and that type of fun stuff). But you can then also access the actually running nodes using RED.nodes.getnode. Some code and comments from the [node-red flexdash plugin](https://github.com/flexdash/node-red-flexdash/blob/main/plugin/flexdash-plugin.js#L138) may be helpful.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [9 May 2022 21:33 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/12 "2022-05-09T21:33:56Z")

</div>

> [@tve](#):
>
> Some code and comments from the [node-red flexdash plugin](https://github.com/flexdash/node-red-flexdash/blob/main/plugin/flexdash-plugin.js#L138) may be helpful.

Your repo is becoming the WikiLeaks of Node-RED 😉  
I think that your code snippet will indeed do the job for me.  
Thanks for sharing these cool ideas!!!

---

<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:** [23 May 2022 21:34 UTC](https://discourse.nodered.org/t/how-to-respond-to-deployments-in-a-plugin/62267/13 "2022-05-23T21:34:55Z")

</div>

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