# Is it possible to create two separate messages in parallel?

**URL:** <https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513>\
**Category:** General\
**Created:** [7 December 2023 21:55 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513 "2023-12-07T21:55:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 December 2023 21:55 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/1 "2023-12-07T21:55:42Z")

</div>

Good evening everyone,

I have a function node that creates a msg.payload and msg.topic.  
I would like to output that msg to output 1 of the function node.

So far no problem.  
Within that same function I would like to create a second msg.payload and msg.topic which should be output to output 2.

So something like

```auto
for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg.topic = "topic 1" + [i]
        msg.payload = "payload 1" + [i]
    node.send([msg, null])
}}

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg.topic = "topic of message 2" + [i]
        msg.payload = "payload 2" + [i] 
    node.send([null, msg])
}}

```

Is that possible?

I amtrying to avoid creating the first message, outputting it via `node.send([msg, null])` and then creating the second and outputting it to out 2 due to the asyncronous behaviour of node-red (since in both cases I will be creating msg.payload and msg.topic and I do not want them to mix and match).

Thank you everyone 🙂  
Alex

---

<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:** [7 December 2023 22:04 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/2 "2023-12-07T22:04:12Z")

</div>

Hi @AleXSR700

I'm not sure I have this right, but is something like this?

you don't have to re-use the same `msg` object - and shouldn't really

```js
for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        
        const Message = {
            topic:`topic 1 ${[i]}`,
            payload: `payload 1 ${[i}`
        }
        
        node.send([Message, undefined])
    }
}

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {

        const Message = {
            topic:`topic 2 ${[i]}`,
            payload: `payload 2 ${[i]}`
        }
        
        node.send([undefined,Message])
    }
}

```

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 December 2023 22:06 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/3 "2023-12-07T22:06:24Z")

</div>

Ah, so maybe I could simply use `msg1.payload` and `msg2.payload`?

So `msg` is not a defined "property" of a flow?

---

<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:** [7 December 2023 22:10 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/4 "2023-12-07T22:10:04Z")

</div>

`msg` is the name of the "package" so to speak

So if you do.

```auto
node.send({someProperty:"Hello"})

```

The next Node along will obtain it with it

`msg.someProperty`

you can manipulate the "package" during its transport through other Nodes, or create a new package, by simply defining a new object (you don't have to call it `msg`) but Node RED will send your package wrapped in a property called `msg` for the next node

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 December 2023 22:12 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/5 "2023-12-07T22:12:29Z")

</div>

Ah, but then I cannot just do

```auto
for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg1.topic = "topic 1" + [i]
        msg1.payload = "payload 1" + [i]
        msg2.topic = "topic of message 2" + [i]
        msg2.payload = "payload 2" + [i] 
    node.send([msg1, msg2])
}}

```

?

---

<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:** [7 December 2023 22:13 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/6 "2023-12-07T22:13:45Z")

</div>

Almost

```auto
// define the packages first
const msg1 = {}
const msg2 = {}

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg1.topic = "topic 1" + [I]
        msg1.payload = "payload 1" + [I]
        msg2.topic = "topic of message 2" + [I]
        msg2.payload = "payload 2" + [I] 
    node.send([msg1, msg2])
}}

```

unless I am not following 😅

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 December 2023 22:17 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/7 "2023-12-07T22:17:00Z")

</div>

No no, you are. That works perfectly, thank you.

So `msg` does not need to be defined but since Node-Red does not know additional messages (msg[i]) I have to use contexts rather than the built-in `msg`.

---

<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:** [7 December 2023 22:20 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/8 "2023-12-07T22:20:07Z")

</div>

> [@AleXSR700](#):
>
> So `msg` does not need to be defined

Not strictly.

but each node downstream will still receive an `msg` object - but each being not related (as you are creating new objects for each output)  
And in that, any properties you added (`payload`, `topic`. etc etc)

```auto
msg.payload
msg.topic

```

if you don't create a new object (package) - all you are doing is manipulating the _current_ `msg` flowing through

I called it a **package** - but was stuck for words 😄

---

<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:** [7 December 2023 22:53 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/9 "2023-12-07T22:53:46Z")

</div>

@AleXSR700

Just realised a slight flaw.

move the object creation inside your loop.  
else the next iteration will just modify the previous `msg1`, and `msg2` object that was sent on the previous iteration.

if you manipulate something that was already sent, the nodes dealing with the previous iteration will see the modifications - you probably don't want that, so create a new object(s) for each loop

```auto
for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {

        const msg1 = {}
        const msg2 = {}

        msg1.topic = "topic 1" + [i]
        msg1.payload = "payload 1" + [i]
        msg2.topic = "topic of message 2" + [i]
        msg2.payload = "payload 2" + [i]

        node.send([msg1, msg2])
    }
}

```

---

<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:** [21 December 2023 22:54 UTC](https://discourse.nodered.org/t/is-it-possible-to-create-two-separate-messages-in-parallel/83513/10 "2023-12-21T22:54:04Z")

</div>

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