# 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:** 1\
**Showing post:** 8

<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 🥂

---

_[View the full topic](https://discourse.nodered.org/t/share-functionality-between-nodes/37717)._
