Node red recieving multiple msg and operation

1,287 views
Skip to first unread message

Anupam Datta

unread,
Oct 20, 2014, 1:50:54 AM10/20/14
to node...@googlegroups.com
For an example I am having msg.payload from 2 function and I want to add them
say I am having 'abc' from function 1
'def' from function 2

the desired output will be 'abcdef'

Now according to node-RED msg receive tutorial 
this.on('input', function(msg) {
        // do something with 'msg'
    });

So when I am creating a new node, how to 'receive' those msgs so that I can do operation on them ie addition concatenation  or whatever else
do I need to write this.on .... twice or it will automatically take ? How to deal with them?

Mark Setrem

unread,
Oct 20, 2014, 3:06:57 AM10/20/14
to node...@googlegroups.com
This looks very similar to the question you asked last month that was called "Handling Multiple Object that come in one instance"
If you read through that long thread you may find the answer you are looking for.

Anupam Datta

unread,
Oct 20, 2014, 3:23:06 AM10/20/14
to node...@googlegroups.com
Not exactly. Not getting the correct answer.

say one function returns ABCD
another EFGH

This is for concatenation:


module.exports = function(RED) {
    function LowerCaseNode(config) {
        RED.nodes.createNode(this,config);
        var node = this;
        this.on('input', function(msg) {
            //msg.payload = msg.payload.toLowerCase();
msg.payload = msg.payload.concat();
            node.send(msg);
        });
    }
    RED.nodes.registerType("lower-case",LowerCaseNode);
}

I am not writing anything inside function node, I am creating one node that accept msgs and then concatenate them all. It may look like my previous query. Because there also I was not able to handle multiple msg and I didn't get my solution.

This is output of debug node 

10/20/2014, 12:48:50 PM[409ff976.bf6008]ABCD
10/20/2014, 12:48:50 PM[409ff976.bf6008]EFGH


As I said ABCD from function node 1
EFGH from function node 2

now I want something like 

10/20/2014, 12:48:50 PM[409ff976.bf6008]ABCDEFGH

ie. just one output after processing it in my concatenation node. But I am unable to do that

My node wiring looks like:

Andrew Lindsay

unread,
Oct 20, 2014, 3:37:12 AM10/20/14
to node...@googlegroups.com
You don't specify the timing of these messages. How far apart are they being received? Minutes? Seconds? Micro seconds? Hours? Days?

Anupam Datta

unread,
Oct 20, 2014, 3:39:19 AM10/20/14
to node...@googlegroups.com
no difference of timing. no delay they are coming together at once

Mark Setrem

unread,
Oct 20, 2014, 3:39:38 AM10/20/14
to node...@googlegroups.com
Can you explain how you think your concatenation function works?

Anupam Datta

unread,
Oct 20, 2014, 3:44:16 AM10/20/14
to node...@googlegroups.com
the msgs will come and then concatenation function will concatenate them and will send.

Andrew Lindsay

unread,
Oct 20, 2014, 4:31:50 AM10/20/14
to node...@googlegroups.com
Node is asynchronous so you won't get the 2 messages in the order you want every time.

Anupam Datta

unread,
Oct 20, 2014, 4:58:43 AM10/20/14
to node...@googlegroups.com
Then also how to concatenate them? Check the thread I have written what I intended to do. It may help you to understand what I meant 
I have given wiring screenshot and example of what I want to do

Antoine Aflalo

unread,
Oct 20, 2014, 5:17:09 AM10/20/14
to node...@googlegroups.com
We are trying to tell you, you can't achieve the concatenation how you want it with Node-RED.

In fact, it's not made to work on 2 messages in the same times. Everything is asynchronous and can then "more or less" happen in the same time. Each message processed by your flow is independent to another.

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Antoine Aflalo

Anupam Datta

unread,
Oct 20, 2014, 5:22:59 AM10/20/14
to node...@googlegroups.com
So how you recommend to do this ?
I am readding:


This is output of debug node 

10/20/2014, 12:48:50 PM[409ff976.bf6008]ABCD
10/20/2014, 12:48:50 PM[409ff976.bf6008]EFGH


As I said ABCD from function node 1
EFGH from function node 2

now I want something like 

10/20/2014, 12:48:50 PM[409ff976.bf6008]ABCDEFGH

ie. just one output after processing it in my concatenation node. But I am unable to do that

My node wiring looks like:


How to achieve that? Because if two msgs to be combined, then it is not possible as per your suggestion.

Mark Setrem

unread,
Oct 20, 2014, 6:23:34 AM10/20/14
to node...@googlegroups.com


Your enthusiasm for Node-Red has to be applauded.  

But to get the most out of Node-Red you really do need to understand some of how javascript works, and it would appear that a lot of the challenges that your a posting about, are down to misunderstanding the basic building blocks of javascript.  Spending some time following an online tutorial on javascript might save you a lot of time and frustration going forward and make the Node-Red documentation easier to follow for you.


The first question you need to ask yourself is why are you sending the inject output to two function nodes to then try to rejoin them again later.  Why not just do it all within one function node and you avoid having the asynchronous message problem?

If you insist on splitting it.  Step one is to store the first value that arrives, and then if and when the second value arrives do something.  The storing of one value and doing something when a second arrives is the same issue as you posted about last month, and if you read the answer to that post there are examples posted there of how you can do the storing part, As has been already pointed out you cannot tell which piece will arrive first.  

If you get this working, then try working on concatenating the two pieces of data together.

Anupam Datta

unread,
Oct 20, 2014, 6:48:56 AM10/20/14
to node...@googlegroups.com
That is example. In real scenario, data may come from several source to several functions which I need to concatenate or do operation on that. 

Anupam Datta

unread,
Oct 20, 2014, 9:03:58 AM10/20/14
to node...@googlegroups.com
I tried with this

var abc = [];
abc.push(msg.payload);
            node.send(abc);
//not working. 

Nicholas O'Leary

unread,
Oct 20, 2014, 11:31:25 AM10/20/14
to node...@googlegroups.com
Anupam,

each node handles one event at a time. You say the messages arrive 'at the same time', but as has been pointed out to you, there is no such thing as 'at the same time' in javascript. They will arrive as two separate calls on the on-input event handler. There is no guarantee as to which order the messages arrive in - your node would have to store each message it receives, decide when it has received all of the messages it needs to concatenate, then, and only then, do the actual concatenation and sending on of the resulting message. How it makes these decisions is up to you. For example, do you need to configure the node with how many messages it should expect, or maybe hard code it to expect only two. Handling the correct ordering of the messages is harder - may you need to attach an additional property onto the message such as an index number that the node can use to get the order right.

This is exactly the scenario I provided you code for last month.


Nick


Anupam Datta

unread,
Oct 21, 2014, 1:58:30 AM10/21/14
to node...@googlegroups.com
So even I use index according to you I have no gurrantee that say element 0 will be in 0th index elemnt 1 will be in index 1 . Because they are not coming in order hence element 0 may come after element 1. Is that true?

Nicholas O'Leary

unread,
Oct 21, 2014, 4:07:22 AM10/21/14
to node...@googlegroups.com
Anupam,

without a concrete example of what you want to do, it is hard to give you exactly the information you are looking for.

Here is an example that concatenates every three messages it receives. It keeps them in the order they are received.

    var messages = [];
    this.on('input', function(msg) {
        // store the received message payload:
        messages.push(msg.payload);
        // if we have three of them stored....
        if (messages.length == 3) {
           // send on a message containing the three payloads concatenated
           this.send({payload: messages.join("")});
           // clear the array of messages ready for the next three to arrive
           messages = [];
        }
    });



Reply all
Reply to author
Forward
0 new messages