# Share functionality between nodes

**URL:** <https://discourse.nodered.org/t/share-functionality-between-nodes/37717>\
**Category:** Developing Nodes\
**Created:** [18 December 2020 17:24 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717 "2020-12-18T17:24:15Z")\
**Posts on this page:** 9\
**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:** [18 December 2020 17:24 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/1 "2020-12-18T17:24:15Z")

</div>

Hi folks,

I developed a long time ago the _ **node-red-contrib-msg-speed** _ node. But I need to develop another node, which needs the reuse most of the internals as the speed node. I would like to share the functionality between both nodes, to avoid having to maintain it twice...

What is the best way to deal which such a situation:

1. Extract that functionality from the speed node and isolate it into a new separate npm package (and let both nodes use that package as a dependency).
2. Other solutions?

Thanks!  
Bart

---

<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:** [18 December 2020 17:52 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/2 "2020-12-18T17:52:04Z")

</div>

Hi Bart,

Publish the "speed code" as an NPM package and include it in your 2 other projects. NPM will deduplicate under the hood.

---

<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:** [18 December 2020 21:38 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/3 "2020-12-18T21:38:45Z")

</div>

Thanks Steve!

And do you have any good practices for such a common NPM package?  
My simplified speed-node package contains this:

```auto
module.exports = function(RED) {
    "use strict";
    var CircularBuffer = require("circular-buffer");
    var Math = require("mathjs");

    function speed(config) {
        RED.nodes.createNode(this, config);
        // Initialization code

        this.on("input", function(msg) {
            // Message handling code
        });
        
        this.on("close",function() {   
            // Cleanup code
        });
    }

    RED.nodes.registerType("msg-speed", speed);
};

```

Do you have any advise on how such a common NPM package could look like (and how I can call it both my nodes)?  
Because most of the code in the 3 code blocks would be common for both nodes, but each node also needs to execute some dedicated code in each block ...

---

<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:** [18 December 2020 21:45 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/4 "2020-12-18T21:45:15Z")

</div>

So which bit is common ? Just the requires ? If so I wouldn’t worry. Just duplicate it and let npm take care of it.

---

<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:** [18 December 2020 21:59 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/5 "2020-12-18T21:59:34Z")

</div>

In fact I mean that every of the 3 blocks (initialization, message handling, cleanup) contain shared code, which should be part of the common npm package. But my both nodes - which depend on this package - should be able to execute their own code in each block.

Probably there is a very simple solution, but I don't see it at the moment ...

---

<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:** [18 December 2020 22:09 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/6 "2020-12-18T22:09:24Z")

</div>

Well if they are related functions (ie also to do with speed) - such that you would ship them both as one package then you can define them both in the same files. If not then not easily no.

---

<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:** [18 December 2020 22:12 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/7 "2020-12-18T22:12:12Z")

</div>

Ok, thanks guys!

---

<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:** [28 December 2020 21:01 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/8 "2020-12-28T21:01:10Z")

</div>

I have now extracted the core functionality from the [node-red-contrib-msg-speed](https://github.com/bartbutenaers/node-red-contrib-msg-speed), and moved it to a new [nr-msg-statistics](https://github.com/bartbutenaers/nr-msg-statistics) package.

For anybody who needs to do something similar in the future, below you can find how I did it. Although there might be many ways to get to Rome ... But since I have a background as object oriented developer, I really wanted to use ES6 classes to accomplish this. So no callback functions for me this time 🙄

1. In the new Github repository, I encapsulated all the shared functionality into a class (which will be exported):

2. The `someMethod` contains my old logic (of the speed node), which I want to reuse in other nodes.  
But at some points in the code I call the (e.g. _ **empty** _) methods `doSomething` and `doSomethingElse`.

3. I added the new npm package as a dependency in the package.json file of my speed node:

4. The speed node now imports this class, and creates a _ **subclass** _ of it. That subclass will _ **override** _ the methods `doSomething` and `doSomethingElse` to do stuff that is specific to this speed node:

So I can reuse the code (in the MessageAnalyzer class) to share it between multiple Node-RED nodes, and each node can overwrite the methods to do stuff that is specific to that node ...

Damn it was fun working with classes after a long time 🥂

---

<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:** [26 February 2021 21:01 UTC](https://discourse.nodered.org/t/share-functionality-between-nodes/37717/9 "2021-02-26T21:01:13Z")

</div>

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