0% found this document useful (0 votes)
74 views2 pages

Node-RED Message Handling Guide

A function can return an array containing multiple messages to send to different outputs. The messages will be sent one by one in the order they were returned. An example shows returning an array with three messages to the first output and one message to the second output. Another example splits an incoming message payload into individual words and returns a message for each word.

Uploaded by

rshegde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views2 pages

Node-RED Message Handling Guide

A function can return an array containing multiple messages to send to different outputs. The messages will be sent one by one in the order they were returned. An example shows returning an array with three messages to the first output and one message to the second output. Another example splits an incoming message payload into individual words and returns a message for each word.

Uploaded by

rshegde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Multiple Messages

A function can return multiple messages on an output by returning an array of


messages within the returned array. When multiple messages are returned for an
output, subsequent nodes will receive the messages one at a time in the order they
were returned.

In the following example, msg1, msg2, msg3 will be sent to the first output. msg4 will be
sent to the second output.

var msg1 = { payload:"first out of output 1" };


var msg2 = { payload:"second out of output 1" };
var msg3 = { payload:"third out of output 1" };
var msg4 = { payload:"only message from output 2" };
return [ [ msg1, msg2, msg3 ], msg4 ];

The following example splits the received payload into individual words and returns a
message for each of the words.

var outputMsgs = [];


var words = [Link](" ");
for (var w in words) {
[Link]({payload:words[w]});
}
return [ outputMsgs ];

Working with sequences

There are a number of core nodes that can work across message sequences:

Split

Turns a single message into a sequence of messages.

The exact behaviour of the node depends on the type of [Link]:

String/Buffer

the message is split using the specified character (default: `\n`), buffer sequence or
into fixed lengths.

Array
the message is split into either individual array elements, or arrays of a fixed-length.

Object
a message is sent for each key/value pair of the object.
Join

Turns a sequence of messages into a single message.

The node provides three modes of operation:

Automatic

attempts to reverse the action of a previous Split node

Manual
allows finer control on how the sequence should be joined

Reduce
New in 0.18 - allows a JSONata expression to be run against each message in the
sequence and the result accumulated to produce a single message.

Sort

New in 0.18

Sorts the sequence based on a property value or JSONata expression result.

Batch

New in 0.18

Creates new sequences of messages from those received.

The node provides three modes of operation:

Number of messages

groups messages into sequences of a given length. The overlap option specifies how
many messages at the end of one sequence should be repeated at the start of the
next sequence.

Time interval
groups messages that arrive within the specified interval. If no messages arrive
within the interval, the node can optionally send on an empty message.

Concatenate Sequences
creates a message sequence by concatenating incoming sequences. Each sequence
must have a [Link] property to identify it. The node is configured with a list of
topic values to identify the order sequences are concatenated.

You might also like