Generate multiple messages with unique _msgid

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.

1 Like

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

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

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

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

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

[{"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":[]}]
2 Likes

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 ?

let myMsg = {};

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

Example

[{"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":[]}]

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

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.

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.

indeed you are right .. my bad

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).

1 Like

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?

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.

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.

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.

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

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