# Access global and/or flow context from a GLOBAL HOOK?

**URL:** <https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457>\
**Category:** Developing Nodes\
**Created:** [16 May 2023 10:21 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457 "2023-05-16T10:21:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [16 May 2023 10:21 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/1 "2023-05-16T10:21:47Z")

</div>

Hi !  
Is there any way to access the global or the flow context in which an activated (and working !) global hook will be fired (for ex. "onSend") ?

All tries (global.get/set, context.global.set/get, context().global.set/get, this.global.get/set, this.context ........) did not work. Any solution or any good idea ?  
Kind regards

---

<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:** [16 May 2023 12:10 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/2 "2023-05-16T12:10:32Z")

</div>

As I asked in your other thread:

> [@How to activate the Global Hooks](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/11):
>
> > [@How to activate the Global Hooks](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/10):
> >
> > and using „var sqlite3=global.get('sqlite3');“ with the error „TypeError: global.get is not a function“.
> 
> 1. Where was this code written? in a function node? in a custom node/plugin

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [16 May 2023 12:18 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/3 "2023-05-16T12:18:57Z")

</div>

sorry - that I've not answered your question.

It's reduced to "const sqlite3 = require('sqlite3').verbose();" - included in a custom node (which works fine meanwhile).

My second question - about accessing context data (global and/or flow) should also been done inside this custom node from which I'm activating hooks (onSend, onCompleted).

---

<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:** [16 May 2023 12:31 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/4 "2023-05-16T12:31:59Z")

</div>

Ok, I've moved this to the #developing-nodes section.

Can I first ask why do you need access to global context? Your own node will have its own closure (for variables that live the life of the runtime) so access to context is not _really_ needed. And imo, not a great idea.

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [16 May 2023 12:41 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/5 "2023-05-16T12:41:08Z")

</div>

What I would like to do is to build a unique identifier for each running flow each time, to write a log-file with all (for me) required information. To cat all information / events - within one running flow each time it will be processed - together (in the logging-file) I will access this unique identifier from all nodes within one flow within one run/process - so my idea is, that accessing this identifier via the hooks via the global/flow context will be the best way. I don't want to modify any node / any flow - thats the reason I'll use the Hook-Events.

---

<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:** [16 May 2023 13:23 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/6 "2023-05-16T13:23:57Z")

</div>

Context can only be accessed via a node in the flow; we don't provide a way to access it directly in a plugin - largely because it hasn't been needed.

Do you need to access this information in multiple places in the code? Or is it entirely within your own hooks? If the latter, why use context when you can just store the information locally yourself?

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [16 May 2023 14:41 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/7 "2023-05-16T14:41:49Z")

</div>

Since the hooks are executed within the nodes in a flow (on the server), I assumed that the "outer context" was also available.

I have integrated several functions within the node that registers the hooks, which are then called by all nodes and create and determine the "UnifiedID", as well as details about the respective node and the assignment to the respective flow (via the "start" file determined) and so process the logging of the necessary data in a SQLITE database.

Of course, I could also register the ID via the SQLITE database and then read out this data in each node, but for me this does not appear to be performant enough and poses imo too many risks in the processes (DB lock, etc.).

Hence the consideration of working with memory variables that are also reset when Node-Red is aborted and restarted.

I hope my thoughts have become understandable and clear. Thank you for your support and the time you are spending.

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [17 May 2023 18:10 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/8 "2023-05-17T18:10:25Z")

</div>

I could still find a solution. I use the option of global variables provided in Node.js in the standard and initialize them in the "onSend" hook. This means that they can be used for all nodes in a flow. I use the ID of the flow as a name of the variable, which ensures uniqueness. Maybe someone else can do something with this. Greetings.

---

<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:** [16 July 2023 18:10 UTC](https://discourse.nodered.org/t/access-global-and-or-flow-context-from-a-global-hook/78457/9 "2023-07-16T18:10:59Z")

</div>

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