# Generate multiple messages with unique \_msgid

**URL:** https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652
**Category:** General
**Created:** [17 August 2020 08:52 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652 "2020-08-17T08:52:37Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![hjalte33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hjalte33/32/27307_2.png) [@hjalte33](https://discourse.nodered.org/u/hjalte33)
#### Post date: [17 August 2020 08:52 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/1 "2020-08-17T08:52:37Z")

</div>

How can I in a function send multiple messages each with a unique \_msgid. So far if i make a loop and use node.send(myMsg). then all the messages have the same message id. I need each message to have it's own unique id.

I have done it before long ago but lost my code and now i'm trying to make it work again, but can't find help anywhere.

---

<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: [17 August 2020 09:48 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/2 "2020-08-17T09:48:49Z")

</div>

If you create a new message each time then it will have a new id  
`let newMsg = {payload: ..., topic: ...}`

---

<div class="post-metadata">

### Author: ![hjalte33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hjalte33/32/27307_2.png) [@hjalte33](https://discourse.nodered.org/u/hjalte33)
#### Post date: [17 August 2020 09:56 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/3 "2020-08-17T09:56:13Z")

</div>

I tried this, but it does not work. they still end up with the same \_msgid

---

<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: [17 August 2020 09:59 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/4 "2020-08-17T09:59:46Z")

</div>

Show us the code, I suspect an error of some sort.

**[Edit]** Use the `</>` button and paste the function here (or the relevant part).

---

<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: [17 August 2020 10:06 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/5 "2020-08-17T10:06:57Z")

</div>

Eh, you are quite right, I think this needs input from @dceejay

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

```auto
[{"id":"c563102b.70a2f","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"MSG","payload":"","payloadType":"date","x":170,"y":2140,"wires":[["f4701fc4.3737b8"]]},{"id":"f4701fc4.3737b8","type":"function","z":"bdd7be38.d3b55","name":"","func":"node.send(msg)\nlet newMsg = {payload: \"test\", topic: \"NEWMSG\"}\nnode.send(newMsg)\nreturn","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":2140,"wires":[["432ff47c.9bd0b4"]]},{"id":"432ff47c.9bd0b4","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":450,"y":2180,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [17 August 2020 10:22 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/6 "2020-08-17T10:22:39Z")

</div>

Hi,

i think **\_msgid** is created by node-red nodes even if if you dont define it to differentiate as to what node sent the message.

**question** : why to modify the **\_msgid** and not create a new object property for your use ?

```auto
let myMsg = {};

for(let i=0; i < 10; i++) {
    
    myMsg = {myid: i, payload: "foo", topic: "bar" }
    node.send(myMsg);
}

```

Example

```auto
[{"id":"3defa83a.e933d","type":"inject","z":"2990ba34.3b9c8e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":400,"wires":[["fcbee6d9.61b95"]]},{"id":"fcbee6d9.61b95","type":"function","z":"2990ba34.3b9c8e","name":"","func":"let myMsg = {};\n\nfor(let i=0; i < 10; i++) {\n \n myMsg = {myid: i, payload: \"foo\", topic: \"bar\" }\n node.send(myMsg);\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":420,"wires":[["8049644e.83ee08"]]},{"id":"8049644e.83ee08","type":"debug","z":"2990ba34.3b9c8e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":420,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![hjalte33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hjalte33/32/27307_2.png) [@hjalte33](https://discourse.nodered.org/u/hjalte33)
#### Post date: [17 August 2020 10:38 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/7 "2020-08-17T10:38:32Z")

</div>

the reason to get different \_msgid is because the receiving node (not created by me) uses the \_msgid to differentiate the messages and it ignores/overwrites the previous command if i use the same \_msgid

---

<div class="post-metadata">

### Author: ![hjalte33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hjalte33/32/27307_2.png) [@hjalte33](https://discourse.nodered.org/u/hjalte33)
#### Post date: [17 August 2020 10:51 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/8 "2020-08-17T10:51:02Z")

</div>

Never mind, I made a mistake. The receiving node did actually get both commands correctly even tho the massages had same id, so i don't need a solution anymore.  
But for future if someone finds this thread it would be nice to know how to actually do this should it become relevant one day. If i remember correctly i did it once before just as stated above by creating a new object, but it seems that's not the case anymore. Or i remember wrongly and it never worked like i thought it did.  
Thanks for the help tho.

---

<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: [17 August 2020 11:04 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/9 "2020-08-17T11:04:36Z")

</div>

> [@UnborN](#):
>
> i think **\_msgid** is created by node-red nodes even if if you dont define it to differentiate as to what node sent the message.

I don't think that is right. The original msg was created by the Inject node whereas the new one is created by the function node so it doesn't identify which node originally sent the message.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [17 August 2020 11:18 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/10 "2020-08-17T11:18:25Z")

</div>

indeed you are right .. my bad

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [17 August 2020 13:18 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/11 "2020-08-17T13:18:54Z")

</div>

Also which node is relying on using different \_msgid values ? Typically the \_ before a name means it is a private variable so should not be used. ( or at least not relied upon).

---

<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: [17 August 2020 13:24 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/12 "2020-08-17T13:24:41Z")

</div>

Can you explain, just for interest, what \_msgid is used for and why it is given the value from the original msg when when `newMsg = {payload: "test"}` is used?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [17 August 2020 13:27 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/13 "2020-08-17T13:27:26Z")

</div>

It it used for trace. If you enable it in setting.js then the trace log could be analysed to track messages through the flow.

---

<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: [17 August 2020 13:33 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/14 "2020-08-17T13:33:57Z")

</div>

OK, thanks, so I deduce that any message created by a node gets given the same id as the message that triggered it, even if multiple new messages are created and sent.

Off Topic, I note that if I reply by clicking the Reply button associated with a post (as I have done here) then, once I have sent it. I am not seeing any indication that this is a reply to that post. I thought I remembered seeing such indication in the past.

---

<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: [17 August 2020 14:03 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/15 "2020-08-17T14:03:05Z")

</div>

> [@Colin](#):
>
> Off Topic, I note that if I reply by clicking the Reply button associated with a post (as I have done here) then, once I have sent it. I am not seeing any indication that this is a reply to that post. I thought I remembered seeing such indication in the past.

Something changed quite a while back. If you respond to the previous message without quoting, it doesn't bother to mark it as a response. Also happens if you try to quote the whole messages, it takes out your quote.

Actually, I can see that even if you do quote, if it is to the immediately previous msg, it doesn't put the link in.

---

<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: [17 August 2020 14:16 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/16 "2020-08-17T14:16:01Z")

</div>

OK, thanks, that fits what I am seeing, and does make sense I suppose.

---

<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: [31 August 2020 14:16 UTC](https://discourse.nodered.org/t/generate-multiple-messages-with-unique-msgid/31652/17 "2020-08-31T14:16:05Z")

</div>

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