# Dynamic name for variables,based for example on \_\_msgid\_\_

**URL:** <https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226>\
**Category:** General\
**Tags:** function-node\
**Created:** [27 November 2023 16:48 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226 "2023-11-27T16:48:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aguida79](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aguida79/32/87228_2.png) [@aguida79](https://discourse.nodered.org/u/aguida79)\
**Post date:** [27 November 2023 16:48 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/1 "2023-11-27T16:48:01Z")

</div>

Hi everyone,  
I want to know if there is a way to define a variable with a dynamic part in the name.

For example, I can define a flow context variable with a dynamic name, like this:

> flow.set('value'+\__msgid_\_,"test");

Or I can define a global context variable with a dynamic name, like this:

> global.set('value'+\__msgid_\_,"test");

Is it possible to do something like this but for a node context variable?

I'm trying to find the best way to deal with concurrency of requests with a HTTP In node, and one idea is to use something that allow me to have different variable names every time that a flow run.

Anyone with the same problem that can share ideas about how to circumvent this?

Thanks in advance for your help.

---

<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:** [27 November 2023 17:02 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/2 "2023-11-27T17:02:12Z")

</div>

Use square bracket property accessors: [Property accessors - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors)

e.g.

```auto
const httpMonitorLookupObject = node.get('httpMonitor') || {}
const propLookup = msg._msgid_ + '_whatever;
const propVal = httpMonitorLookupObject[propLookup]

// setting a value
httpMonitorLookupObject[propLookup = {}

// storing it all as a single object
node.set('httpMonitor', httpMonitorLookupObject)

```

---

<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:** [27 November 2023 17:21 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/3 "2023-11-27T17:21:13Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> const httpMonitorLookupObject = node.get('httpMonitor') || {}
> const propLookup = msg._msgid_ + '_whatever;
> const propVal = httpMonitorLookupObject[propLookup]
> 
> // setting a value
> httpMonitorLookupObject[propLookup = {}
> 
> // storing it all as a single object
> node.set('httpMonitor', httpMonitorLookupObject)
> 
> ```

Things ok at home @Steve-Mcl? 😉

```auto
const httpMonitorLookupObject = context.get('httpMonitor') || {}
const propLookup = msg._msgid_ + '_whatever';
const propVal = httpMonitorLookupObject[propLookup]

// setting a value
httpMonitorLookupObject[propLookup] = {}

// storing it all as a single object
context.set('httpMonitor', httpMonitorLookupObject)

```

- `'_whatever` **vs** `'_whatever'`
- `[propLookup = {}` **vs** `[propLookup] = {}`
- `node.set/get` **vs** `context.set/get`

unless I'm missing something, then ignore me 😅

---

<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:** [27 November 2023 17:58 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/4 "2023-11-27T17:58:48Z")

</div>

Was an untested rush job 😂

Tbf, I don't like making mistakes, this was just sloppy. I am blaming recent I'll health (man flu) and lack of 💤

@aguida79 ignore my post and follow @marcus-j-davies

---

<div class="post-metadata">

**Author:** ![aguida79](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aguida79/32/87228_2.png) [@aguida79](https://discourse.nodered.org/u/aguida79)\
**Post date:** [27 November 2023 21:45 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/5 "2023-11-27T21:45:46Z")

</div>

Even with this idea, I cannot find the way to push the different msg.payload messages that are being generated from every node of the workflow to the specific/corresponding httpMonitorLookupObject[propLookup] key, maybe because I'm not a good Javascript coder, ☹

Is there a simpler/better way to try to manage concurrency with a flow that run every time that receive an HTTP In, an that events can be received many of them in a few seconds?

Thanks in advance for your help.

---

<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:** [27 November 2023 21:56 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/6 "2023-11-27T21:56:49Z")

</div>

You can use `msg.req.ip` ?

The `HTTP IN` node will include the clients IP address

```auto
// Get Client States
let clients = context.get("clientState")
if(!clients){
    context.set("clientState",{})
    /* we do this to shift our reference to the context */
    clients = context.get("clientState")
}

// Get State for client (Based on their IP), or create a fresh state for the client if not yet known
const clientState = clients[msg.req.ip] || {}

// Set a property
clientState.someProperty = "Hello, World"

// Get a property
const someProp = clientState.someProperty

// Store it (just in-case this is a new client)
clients[msg.req.ip] = clientState

// Remove a clients state if needed
delete clients[msg.req.ip] 

```

You can then pass on the `clientState` downstream

```auto
msg.state = RED.util.cloneMessage(clientState) /* <-- important, to stop it being modified during transit */
return msg

```

---

<div class="post-metadata">

**Author:** ![aguida79](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aguida79/32/87228_2.png) [@aguida79](https://discourse.nodered.org/u/aguida79)\
**Post date:** [27 November 2023 22:40 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/7 "2023-11-27T22:40:07Z")

</div>

Thanks @marcus-j-davies for your reply.  
The problem in my use case is that the clientIP is going to be always the same, a FluentBit collector that is sending DNS log events by an HTTP Output to be enriched by the NodeRED workflow that I created.

---

<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:** [27 November 2023 22:44 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/8 "2023-11-27T22:44:35Z")

</div>

> [@aguida79](#):
>
> a FluentBit collector that is sending DNS log events by an HTTP Output

Try `msg.req.connection.remotePort`

The port, should normally not remain the same for each request.  
But that will be based on how FluentBit behaves

 ![Screenshot 2023-11-27 at 22.43.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a7af5af3987dff51a62ea46bfc90bd369be72566.png)

At some point, the port will wrap around again - but that is something you will need to manage

---

<div class="post-metadata">

**Author:** ![aguida79](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aguida79/32/87228_2.png) [@aguida79](https://discourse.nodered.org/u/aguida79)\
**Post date:** [27 November 2023 23:30 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/9 "2023-11-27T23:30:22Z")

</div>

Thanks @marcus-j-davies , I'm going to try with that option.

---

<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:** [26 January 2024 23:30 UTC](https://discourse.nodered.org/t/dynamic-name-for-variables-based-for-example-on-msgid/83226/10 "2024-01-26T23:30:37Z")

</div>

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