# Sharing a single instance

**URL:** https://discourse.nodered.org/t/sharing-a-single-instance/10342
**Category:** Developing Nodes
**Created:** [18 April 2019 20:06 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342 "2019-04-18T20:06:48Z")
**Posts on this page:** 8
**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 April 2019 20:06 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/1 "2019-04-18T20:06:48Z")

</div>

Hi folks,

I'm redesigning my node-red-contrib-http-logger to use the [mitm](https://www.npmjs.com/package/mitm) library for intercepting http(s) traffic. However after debugging a few evenings, it became at the end clear that _ **I'm only allowed to create 1 mitm instance** _:

```auto
var Mitm = require("mitm");
var mitm = new Mitm();

```

A user should be able to add **multiple** logger nodes to his flow (node1 to listen for requests to host1, node2 to listen for requests to host2, ....). But they all have to **share** this single instance.

In Java I would simply create a _singleton_, but not sure to do it in Javascript for Node-RED. Can anybody share please a code snippet how to accomplish this?

Thanks !  
Bart

---

<div class="post-metadata">

### Author: ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)
#### Post date: [19 April 2019 09:36 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/2 "2019-04-19T09:36:01Z")

</div>

maaybe there are better ways, but you could store this object in the global context under a very unique name. If the object does not exist, then you would create it.

---

<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: [19 April 2019 10:02 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/3 "2019-04-19T10:02:18Z")

</div>

each node has its own context... no need to pollute the global one.

---

<div class="post-metadata">

### Author: ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)
#### Post date: [19 April 2019 11:27 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/4 "2019-04-19T11:27:59Z")

</div>

> [@dceejay](#):
>
> each node has its own context... no need to pollute the global one.

according to Barts comment

> [@BartButenaers](#):
>
> However after debugging a few evenings, it became at the end clear that _ **I'm only allowed to create 1 mitm instance** _ :

one can have only one mitm instance in the node process

---

<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: [19 April 2019 11:49 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/5 "2019-04-19T11:49:54Z")

</div>

Anything that you put outside the function you include in `RED.nodes.registerType()` (param 2) is common to all instances of your node.

If you need multiple instances, they have to go inside that function. You also have to remember to destroy things if they are set up inside that function using `node.on('close', ...)` otherwise you will probably end up with memory leaks and other undesirable side effects).

In uibuilder for example, I use this to create a single logging instance for all instances of the node. v2 also uses this to improve efficiency by adding ExpressJS middleware routes for loaded libraries only once for all instances. Similarly, API's are only set up once no matter how many instances of uibuilder you have in your flows.

---

<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: [19 April 2019 21:30 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/6 "2019-04-19T21:30:34Z")

</div>

@cinhcet, @dceejay,  
Thanks but I thought using (node/flow/global) contexts wouldn't be a good solution in this case. Suppose I would do it like this:

```auto
var mitm = new Mitm();
this.context().global.set(mitm);

```

Inside that constructor the whole interception stuff is being setup. So what happens if a Node-RED contextstore is storing and loading that instance? Therefore I thought it would be better to avoid using the context???

> [@TotallyInformation](#):
>
> Anything that you put outside the function you include in `RED.nodes.registerType()` (param 2) is common to all instances of your node.

@TotallyInformation: Suppose I do it like this:

```auto
module.exports = function(RED) {
    var Mitm = require("mitm");
    
    var mitm = new Mitm();

    function HttpLoggerNode(config) {
        RED.nodes.createNode(this, config);

   }

    RED.nodes.registerType("http-logger",HttpLoggerNode);
}

```

Do you mean that the "mitm" variable is only initialized once (i.e. constructor is only executed once), and available to all the HttpLoggerNode's in my flow?

---

<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: [19 April 2019 21:49 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/7 "2019-04-19T21:49:05Z")

</div>

> [@BartButenaers](#):
>
> Do you mean that the "mitm" variable is only initialized once (i.e. constructor is only executed once), and available to all the HttpLoggerNode's in my flow?

Correct. The function your module exports gets called _once_ when Node-RED starts up and loads you module. So the mitm function will only be called once and will be in-scope for all code with the `function(RED) { }`.

If you are able to detect if mitm has been initialised or not, you _might_ want to add some error handling and reporting just in case some other module uses mitm and gets there first.

---

<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: [20 April 2019 00:24 UTC](https://discourse.nodered.org/t/sharing-a-single-instance/10342/8 "2019-04-20T00:24:22Z")

</div>

> [@BartButenaers](#):
>
> Do you mean that the "mitm" variable is only initialized once (i.e. constructor is only executed once), and available to all the HttpLoggerNode's in my flow?

🙂

Really, you should do the require outside the exports since the Node-RED service doesn't need access to it directly. Indeed, I think that `var mitm ...` can be outside the exports as well. Though I would make that a `const mitm ...` myself. Same for the require statement. Putting things outside the exports should mean that they can't be accidentally accessed by anything else in Node-RED other than your node type itself. Might not make any difference really given how Node-RED works but good defensive programming style anyway.
