# Getting a unique ID for each node (that doesnt change)

**URL:** <https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575>\
**Category:** Developing Nodes\
**Created:** [15 January 2024 19:26 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575 "2024-01-15T19:26:04Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruceb85](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bruceb85](https://discourse.nodered.org/u/bruceb85)\
**Post date:** [15 January 2024 19:26 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/1 "2024-01-15T19:26:04Z")

</div>

I am trying to build an and gate, without using topics to distinguish the inputs. I would like multiple inject nodes to fire a boolean value to the and gate. If all are true then it outputs true. However, it seems that there is no way to differentiate between the node instances. \_msg.id returns a unique id for each occurence. nodeid returns the same id for each node type. What I am looking for is a unique id which references the static node which sent the signal, so that the AND get can store the associated boolean value in a dictionary. I was wondering if there is another way to get this unique id, of if this is not how node-red is intended to be used.

---

<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:** [15 January 2024 19:33 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/2 "2024-01-15T19:33:54Z")

</div>

On the basis you're developing a Custom Node for Node RED (one that users can install)

You can take advantage of the hooks API.  
[https://nodered.org/docs/api/hooks/messaging/](https://nodered.org/docs/api/hooks/messaging/)

You can subscribe to the event that is triggered when a Message is received,  
and if its been received by the Node ID, that this hook resides, store the source Node ID that sent it

Example of the event ( `SendEvent`)  
check if `destination.id` === `this.id` and store `source.id` and use as you see fit

```auto
{
    "source": {
        "id": "<node-id>",
        "node": "<node-object>",
        "port": "<index of port being sent on>",
    },
    "destination": {
        "id": "<node-id>",
        "node": undefined,
    },
}

```

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [15 January 2024 19:37 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/3 "2024-01-15T19:37:24Z")

</div>

> [@bruceb85](#):
>
> nodeid returns the same id for each node type

Not true, each node has a unique id (`node.id`)

---

<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:** [15 January 2024 19:41 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/4 "2024-01-15T19:41:08Z")

</div>

`node.id` will always be the `id` of `this` - so in that respect it is true.  
to get the node id of the source node - you need to use the Hooks API I provided above.

---

<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:** [15 January 2024 19:49 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/5 "2024-01-15T19:49:43Z")

</div>

> [@bruceb85](#):
>
> If all are true then it outputs true. However, it seems that there is no way to differentiate between the node instances.

Yes, there is. Every node has a unique `id` already.

> [@marcus-j-davies](#):
>
> `node.id` will always be the `id` of `this` - so in that respect it is true.

Sorry Marcus, not sure what you are saying there. The node instances ID _has_ to be unique at all times or Node-RED wouldn't work.

In the runtime or the Editor code, `this.id` is the ID of the instance of the node. The hooks API is not required.

---

<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:** [15 January 2024 19:56 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/6 "2024-01-15T19:56:06Z")

</div>

So....  
How do you get the ID of the node that sent the message?  
Calling `this.id` does not return the node id that sent the message.

Example....  
This function node cannot get the ID of the inject that sent it  
 ![Screenshot 2024-01-15 at 19.55.47](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/305a2d6999af890438e7b31bd836ca833f993f90.png)

(this is for a custom node)  
and as far as I know it, to get the sending ID `hooks` is needed

The OP wants the Node ID that sent the message, so they can use it as a type of state object for source Node sending the message

---

<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:** [15 January 2024 20:10 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/7 "2024-01-15T20:10:25Z")

</div>

> [@marcus-j-davies](#):
>
> This function node cannot get the ID of the inject that sent it

But this is in the "Developing nodes" category. As such, not about function nodes.

However, you absolutely can get the node's ID in a function node, simply by referencing `node.id`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/767cc74a79070b1c85336da6d8c2fbbb04a700a6.png)

In a function node, `this` is not relevant in the outer code of the nodes function because that context is `globalThis` as it is a node.js VM.

Put this code into the top level of a function node's code:

```auto
node.warn(globalThis === this)

```

The output will show `true`.

---

<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:** [15 January 2024 20:23 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/8 "2024-01-15T20:23:28Z")

</div>

> [@bruceb85](#):
>
> I would like multiple inject nodes to fire a boolean value to the and gate

> [@bruceb85](#):
>
> without using topics to distinguish the inputs

To obtain the source ID (automatically) without having to worry about it, in a custom node.  
i.e an Inject Node sending a message to the OP's custom node as per requirement.

I am unaware of how without Hooks

Yes, using a function node to send it, you include `node.id` in the message  
(sorry I used `this.id` to refer to `node.id`)

but then, its not the inject node that is sending the message, its a function node

Unless the OP used `inject` as an example 🤔

---

<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:** [15 January 2024 20:33 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/9 "2024-01-15T20:33:20Z")

</div>

> [@marcus-j-davies](#):
>
> Yes, using a function node to send it, you include `node.id` in the message  
> (sorry I used `this.id` to refer to `node.id`)
> 
> but then, its not the inject node that is sending the message, its a function node

I that situation, I don't believe you _actually_ need the source node, only to be able to differentiate? Probably doesn't matter _what_ the id is as long as the two inputs can be differentiated?

In that case, all that is needed is for each separate input to generate an id unique to the input path along the way. That could simply be anything injected in a change node.

* * *

Noting that if this WERE a custom node question, then you _can_ (but generally _shouldn't_) find the upstream node ID.

* * *

UPDATE: Actually, I gave up too early - you CAN find the upstream node id in a function node so you only actually need a single one. The `node.path` contains the upstream node id. But, as mentioned, nodes and node instances should not - generally - want to know this.

---

<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:** [15 January 2024 20:43 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/10 "2024-01-15T20:43:02Z")

</div>

> [@TotallyInformation](#):
>
> you CAN find the upstream node id in a function

Close but no cigar 😄

`path` is FlowID -\> Current Node (I don't think it traverses the message path)

(each calls `node.path`)

 ![Screenshot 2024-01-15 at 20.42.24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d36c84f2403176b3ed40d5cdf6bde09432dfde8c.png)

Probably a good thing

---

<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:** [15 January 2024 20:51 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/11 "2024-01-15T20:51:20Z")

</div>

> [@marcus-j-davies](#):
>
> Close but no cigar 😄

Drat, note to self - stop looking at the Node-RED forum after a day a work! One assumption too many.

> [@marcus-j-davies](#):
>
> Probably a good thing

Yup. It can be done in a custom node but it isn't straight-forward. And certainly isn't recommended.

* * *

Anyway, if not talking about custom nodes, there are simpler ways, as mentioned.

---

<div class="post-metadata">

**Author:** ![bruceb85](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bruceb85](https://discourse.nodered.org/u/bruceb85)\
**Post date:** [15 January 2024 20:52 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/12 "2024-01-15T20:52:21Z")

</div>

I am trying to use a hook to receive the source id of the previous node. In this case an inject node. I am getting this error: TypeError: Cannot read properties of undefined (reading 'id'). How should I access the source id for the receiveEvent?

```auto
// Example postReceive hook
        RED.hooks.add("postReceive", (receiveEvent) => {
            node.warn(`Message received: ${receiveEvent.msg.payload}`);//true
            node.warn(`Message id: ${receiveEvent.source.id}`);//TypeError: Cannot read properties of undefined (reading 'id')
            
        });

```

---

<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:** [15 January 2024 20:58 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/13 "2024-01-15T20:58:27Z")

</div>

`source` is only with :

- onSend
- preDeliver
- postDelivery

---

<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:** [15 January 2024 21:24 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/14 "2024-01-15T21:24:56Z")

</div>

> [@bruceb85](#):
>
> I was wondering if there is another way to get this unique id, of if this is not how node-red is intended to be used.

This is not how NR is intended to be used... at least, not in the way you're trying to do it.

Nodes should not care about who sent them a message; they should care about the message they receive. It would be more Node-RED-like for each Inject node to set `msg.topic` (for example) and for you to map the boolean against that value - without regard for what node sent it.

The hooks API isn't really intended for this purpose and I'd advise against going down that route.

---

<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:** [16 January 2024 08:33 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/15 "2024-01-16T08:33:30Z")

</div>

Sorry, that was my fault, was too taken in of the challenge.  
but yes, I wouldn't do this either, and is not a Node RED way.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [16 January 2024 08:39 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/16 "2024-01-16T08:39:54Z")

</div>

That being said, there are custom nodes for logic gates that already use that approach by `topic`. No need to reinvent the wheel. 🙂

Have a look at: [https://flows.nodered.org/node/node-red-contrib-boolean-logic-ultimate](https://flows.nodered.org/node/node-red-contrib-boolean-logic-ultimate)

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [16 January 2024 22:17 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/17 "2024-01-16T22:17:39Z")

</div>

> [@marcus-j-davies](#):
>
> and is not a Node RED way

Its like watching The Mandalorian 😂

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [17 January 2024 08:38 UTC](https://discourse.nodered.org/t/getting-a-unique-id-for-each-node-that-doesnt-change/84575/18 "2024-01-17T08:38:08Z")

</div>

> [@Sean-McG](#):
>
> Its like watching the The Mandalorian 😂

This is the way! 🤓
