Changing _msgid

I was looking at this old question (here) and have a related question.

If I want to create new msgids on messages, can I do that?

For instance, if I get an array of devices and want to start up a process for each of those devices, I don't think I want each of the messages to have the same _msgid.

I can iterate through the list of devices and create a new message for each of them inside a function node.
But how can i change the _msgid?

I tried a few things and so far the _msgid remains stubbornly the same.

H

Why not just use a msg.device property?

It's possible/likely that I don't understand the significance of the _msgid.

Each message would have its own unique payload, related to the device (a uuid).

I just don't want the messages getting mixed up anywhere down the line, so it seemed sensible to have unique _msgid. The messages would flow down into logic flows where the data for each device would be gotten from a db and calculations done, with optional flows based on the logic.

That's more or less why I thought that unique _msgid would be a good idea.

I hope that makes sense!

H

If you assign the uuid to msg.uuid for example you preserve the uniqueness of each message and process them accordingly. This is exactly the same as having unique _msgid.

1 Like

If you use a split node to split your array you will get a discrete message for each array element.

I don't actually know because I've never cared to check but presumably each message has it's own unique msgid?

That sounds good.

So with this, and where each device name is a uuid

// Input message:
// msg.payload = ["device1", "device2", "device17", "device10", "device2223"]

// Extract the array of chains from the message payload
var devices = msg.payload;

// Iterate over each chain and send a new message for each
for (var i = 0; i < devices.length; i++) {
  var device = devices[i];

  // Create a new message object
  var newMsg = {
    payload: device
  }

  // Send the new message to the next node
  node.send(newMsg);
}

I don't have to worry that the messages get mixed up?
Or should I add something like this?

 var newMsg = {
    payload: device
    uuid: device
  }

or just leave out the payload entirely and have

 var newMsg = {
    deviceId: device
  }

Again, thanks!

H

Yes. That works.

Each new message gets a unique _msgid.

The split node also adds a section called msg.parts. I'm not sure what that's for.

Thanks very much!

H

It is to help the join node re-assemble the stream of split messages back into an array.

1 Like

Ah! That makes perfect sense. Thanks. Could be very useful later on, actually!

H

If your main programming knowledge is procedural code, every Node-red problem looks like a function problem.

I know it doesn't roll off the tongue like if you have a hammer every problem is a nail but no less wise. :crazy_face:

I tend not to worry too much about the programming paradigm....though I tend not to like Object Oriented unless the objects represent something clearly actually physical, or at least elements in a UI. Otherwise? Not so much.

In any case, thanks for the pointer!

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