# Good usage of context.flow

**URL:** <https://discourse.nodered.org/t/good-usage-of-context-flow/20613>\
**Category:** General\
**Created:** [20 January 2020 09:17 UTC](https://discourse.nodered.org/t/good-usage-of-context-flow/20613 "2020-01-20T09:17:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![etawyy](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@etawyy](https://discourse.nodered.org/u/etawyy)\
**Post date:** [20 January 2020 09:17 UTC](https://discourse.nodered.org/t/good-usage-of-context-flow/20613/1 "2020-01-20T09:17:35Z")

</div>

I have a flow with lots of nodes and calls to my DBs, and I need to use at the end of the flow a field that was pulled from a DB at the beginning of the flow.

So far I have been passing the value all along the message flow which is a bit inefficient and complex (at the end of the flow my msg is clogged with many temporary information).

I have read that the flow context might be the solution, but one thing is unclear: is the flow object unique for all messages that enter my flow or is it unique per message?

eg message 1 comes, I store and use flow.siteId.  
message 2 comes, I will also store and use flow.siteId, but is the previous flow.siteID still there?

I will end up with many messages coming simultaneously, so if flow.siteID is a single variable used by all messages, it will be an issue.

---

<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:** [20 January 2020 09:21 UTC](https://discourse.nodered.org/t/good-usage-of-context-flow/20613/2 "2020-01-20T09:21:00Z")

</div>

> [@etawyy](#):
>
> eg message 1 comes, I store and use flow.siteId.  
> message 2 comes, I will also store and use flow.siteId, but is the previous flow.siteID still there?

No, that will overwrite the previous value. The `flow` context is shared by all nodes on the current tab.

---

<div class="post-metadata">

**Author:** ![etawyy](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@etawyy](https://discourse.nodered.org/u/etawyy)\
**Post date:** [20 January 2020 09:28 UTC](https://discourse.nodered.org/t/good-usage-of-context-flow/20613/3 "2020-01-20T09:28:09Z")

</div>

> [@kuema](#):
>
> comes, I store and use flow.siteId.  
> message 2 comes,

ok thanks, so I should not use it for temp values along my flow.  
Is that the best practice, to just store some msg.tempData all along the flow to reuse them when I need it later in the flow ?

---

<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:** [20 January 2020 09:42 UTC](https://discourse.nodered.org/t/good-usage-of-context-flow/20613/4 "2020-01-20T09:42:06Z")

</div>

Your flow can be triggered multiple times, meaning multiple independent message objects. So everything you need along a single flow execution (temp values) should be carried by the message object.

The flow context should be used if you need to share data between independent flow executions, e.g. for aggregation, counting, ...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 January 2020 09:56 UTC](https://discourse.nodered.org/t/good-usage-of-context-flow/20613/5 "2020-01-20T09:56:09Z")

</div>

> [@etawyy](#):
>
> store some msg.tempData all along the flow

That is what I would do, though I would give it a meaningful name. All information in a message is temporary. You said at the start that this is inefficient and complex. There is little impact on efficiency as usually a message is passed along the flow by reference, nothing is actually moved about, it does not keep creating recreating messages. The message stays where it is and a pointer to it is passed along. It should not be complex if you structure msg.tempData in a meaningful way.  
An alternative which may be appropriate is to send the extra data off on another wire bypassing all the nodes between and use a Join node in Key/Value pairs mode or Merge mode to join it back in when you need it.
