Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Multiple asynchronous public API calls (rails+node.js or reactive js)

Multiple asynchronous public API calls (rails+node.js or reactive js)

Problem

I'm trying to make non-blocking calls to 3 public APIs, i.e website A,B,C and then forward the results back to the rails app as JSON datas. I asked if this is possible in node.js on another forum and it seems it is and someone pointed me to this solution that involves using node.js Step module and async library:

Step( 
// Make 3 async calls in parallel 
  function loadStuff() { 
    getResultFromSiteA(params1, this.parallel()); 
    getResultFromSiteB(params2, this.parallel()); 
    getResultFromSiteC(params3, this.parallel()); 
  }, 
// Pass the result to Rails when you're done 
  function passOntoRails(err, resultsA, resultsB, resultsC) { 
    if (err) { throw err; } 
    passResultsToRails(resultsA, resultsB, resultsC); 
  } 
)

Recently I also found similar question here. The answer suggests using forkjoin operator available within js extension I've never heard of; 'reactive js'.

So from what I can understand there's 2 ways of doing this; the first one through node.js and the second way is through simple multiple asynchronous ajax calls from client side using 'reactive'.

I'd like to know if one way simply performs better/faster than another? thanks. any opinions/answers/suggestions would be appreciated.

Problem courtesy of: Benny Tjia

Solution

Well the idea is the same, but the first approach is for the server (Node.js) and the second one is for the browser (which you don't need in this case).

Since you have N asynchronous tasks that need to be resolved and only then (after all the tasks are executed and the results returned) can you send the data back to Rails, then using either Step or Async is fine.

How do they work behind the scenes? Well you have N tasks and after each task is resolved N becomes N-1 and so on, until N == 0 and then the callback function gets executed with the desired data.

Read more about flow control in Node.js here:

http://howtonode.org/control-flow
http://howtonode.org/step-of-conductor
http://dailyjs.com/2011/11/14/popular-control-flow/

Solution courtesy of: alessioalex

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Multiple asynchronous public API calls (rails+node.js or reactive js)

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×