How to work around calling async function in a function node

2,558 views
Skip to first unread message

regis piccand

unread,
Jun 11, 2014, 3:19:53 AM6/11/14
to node...@googlegroups.com
Hey guys,

I'm starting with Node Red, trying to retrieve data from InfluxDB.

I do that in a function node; however, the problem is that reading from InfluxDB is an async call.

As such, the function node exits before the callback function is executed. As I read in a thread, async calls are not supported in function node, yet.

Are there any workarounds? How could I be calling an async function within Node Red ?

Thanks
-Regis

Dave C-J

unread,
Jun 11, 2014, 3:41:22 AM6/11/14
to node...@googlegroups.com
Hi,
I don't think there are any easy workarounds at present. The two ways that come to mind are 
a) use the exec node to shell out to an external command to get the data (bad for performance but....)
b) actually create an InfluxDB node based on our existing MySQL node (for example)  and using something like https://github.com/bencevans/node-influx - instead of the mysql driver.
That way you would have full control of what was returned and when.


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



--
regards

Dave Conway-Jones

Dave C-J

unread,
Jun 11, 2014, 3:44:48 AM6/11/14
to node...@googlegroups.com
Looking at the InfluxDB docs some more there seems to be a well supported http interface - http://influxdb.com/docs/v0.7/api/reading_and_writing_data.html - so you could use the http request node instead... that may be the simplest workaround for simple reads and writes.
--
regards

Dave Conway-Jones

regis piccand

unread,
Jun 11, 2014, 4:03:31 AM6/11/14
to node...@googlegroups.com
Dave, Thanks a lot for your reply. I guess both options will work for me. As I am evaluating Node Red, this is a good opportunity to try and create my own InfluxDb node!

Best,
Regis

Lawrence Griffiths

unread,
Jun 12, 2014, 3:17:21 PM6/12/14
to node...@googlegroups.com
Regis InfluxDb looks very interesting.

I've been using SQLite locally and have sometimes used tempoDB for cloud time series storage.
But would be more thank happy to do some testing if that's any use to you.

Lawrence

regis piccand

unread,
Jun 12, 2014, 5:28:01 PM6/12/14
to node...@googlegroups.com

Hi Lawrence, thank for the offer! I managed to create  new node, specifically for connecting and retrieving some test data. Works well. I had to use some mechanism to let the callback finish before sending the message forward. The wait.for did the trick for me. I'm happy to post  this code if you feel it can be useful.

Thanks,
Regis

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/9gNvMhAZkJ4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

Luis Montes

unread,
Jun 12, 2014, 9:48:19 PM6/12/14
to node...@googlegroups.com
I've got a couple of forks with async functionality in the function node.  Pretty straight forward, just add "send" in the parameter list and export it to the sandbox.  Then your function can call send(msg) or send([msg1,msg2]) at any point and as many times as you wish.  It's been extremely useful.

I could attempt a pull request if anyone is interested.

regis piccand

unread,
Jun 13, 2014, 4:51:44 AM6/13/14
to node...@googlegroups.com
Hi Luis,

I would definitely be interested. not sure how pull requests work, as I am new to GitHub. My home is here: https://github.com/rpiccand

Thanks,
Regis

Nicholas O'Leary

unread,
Jun 13, 2014, 8:12:13 AM6/13/14
to node...@googlegroups.com
Hi,

we've had this conversation before and whilst I do want to enable async functionality in the function node, I'm not sure this is quite the way I want to do it.

Specifically, its the bit about being able to call send() "at any point and as many times as you wish". I had in mind more of an approach that still allows async completion, but only once per received message - think of it more as returning a promise to send a message (or array of messages..). This is consistent with the model of a function node being a mid-flow node - it does something in reaction to receiving a message.

That said, I'm willing to be convinced otherwise. Ultimately, I think I need to try out the two different approaches and see how they feel.

Nick






TJ Koury

unread,
Jun 17, 2014, 4:52:04 PM6/17/14
to node...@googlegroups.com
This is one of the things I want to enable in the Redis-In node using native Redis pub/sub.  Maybe have a 'queue' node with a list of outputs tied to a subscription node or something?

Rayner Vintervoll

unread,
Jul 12, 2014, 4:20:34 PM7/12/14
to node...@googlegroups.com
We have this requirement in a project and I have created a version of the core function node that supports asynchronous functions. https://www.npmjs.org/package/node-red-contrib-async

I'm quite new to node-red development so I'm very curious about what you guys think of the solution.

Basically the node has a predefined callback (cb) that users can call and that handles messages the same way returned messages are. I haven't implemened any constraints that ensure that the callback is called only once (as Nicholas commented above perhaps should be enforced), but that should be quite easy to implement. 

Additionally I have included the async.js library in the sandbox context, which makes it possible to write quite elegant functions that perform multiple asychronous operations that finally into an operation that call the predefined callback that continues the flow. An example:

async.parallel({
  one: function(join){
    _.delay(function(){
 join(null, "Hello ");
}, 2000);
  },
  two: function(join){
    _.delay(function(){
 join(null, "World");
}, 2000);
  }
}, function(err, results) {
  // results is now equals to: {one: "Hello", two: "World"}
  msg.payload = results.one + results.two;
  cb(msg);
});

Nicholas O'Leary

unread,
Jul 12, 2014, 5:01:29 PM7/12/14
to node...@googlegroups.com
Thanks for this Rayner.

FYI, we do plan to add async capability to the core function node eventually.

Nick


--

Luis Montes

unread,
Jul 14, 2014, 11:41:44 AM7/14/14
to node...@googlegroups.com
FWIW, I'd rather the function node looked for thenable results than to have to resort including the async lib.  Maybe even default to putting when.js in global :)

Oliv'

unread,
Jan 25, 2015, 4:51:22 AM1/25/15
to node...@googlegroups.com, *...@piccand.com


On Friday, June 13, 2014 at 10:51:44 AM UTC+2, regis piccand wrote:
I would definitely be interested. not sure how pull requests work, as I am new to GitHub. My home is here: https://github.com/rpiccand

Hi Regis,

Dave said me that you worked a bit on a node for InfluxDB. Did you made any progress ?

Thanks
Oliv' 
Reply all
Reply to author
Forward
0 new messages