Let's say I have a few services, Service A that does Invoicing, Service B that does Payments, and the system has to communicate with 2-3 web-services as well. Let's say there is a need to create some sort of "portal"-ish solution (mobile or not). In this situation, what would be the advantage using Node?
Would you mind to share when Node is not the right choice as well?
I'm interested to know more about Node :)
UPDATE: Thank you for the replies.
FYI, I do understand the concept of node.js in terms of the async callback and the argument of client-side and server-side use the same code.
I'm not a heavy Rails user (more of a Java/Python guy) but when it comes to creating a typical CRUD web-app, Rails would be more productive?
For example, say your portal needs to collect info from 3 sources A, B, and C. In a synchronous system, you would have to request service A, wait for a response, then request B, wait, and so on. This means your request is utilizing the system for all that time, unable to serve other requests, without extra threads or processes.
Node, and other async systems, when they call for IO, like a service, a callback is registered for the result. So instead of having to wait for the result, the system becomes available for other actions, like other requests. So in this case you could request service A, B, and C all at once, and while you are waiting for the responses, the system can be handling other requests. When any of those services completes, it calls back to your code so you can handle the results, and give a response to the client.
So the advantage is that instead of a request taking A + B + C + extra time, it can take max(A, B, C) + extra time to serve the request, while serving other requests concurrently.
Node is not the only way to achieve this, many async systems exist like Tornado for python, EventMachine for ruby, and many others. But the JavaScript in node can be particularly fun to work with especially if you are also doing the front-end JavaScript, as it pretty much brings the context-switching in your brain to almost nothing.
For instance, if you switch from Rails to EventMachine, will you get most of the benefits of Node, or will you still be bogged down by Ruby's speed and resource consumption?
function get(source, callback) {
var result = getData(source);
callback(result);
}
get(A, function(result) {
get(B, function(result) {
get(C, function(result) {
// CREATE RESPONSE WITH A, B, AND C HERE
});
});
});
Wouldn't you have to wait for the data to be retrieved before running the next callback resulting in a time of A + B + C anyways? Am I missing something about the way to retrieve data from multiple sources? I don't see how max(A, B, C) is possible while still knowing for sure when all the data has been collected.https://github.com/joyent/node/wiki/modules#wiki-async-flow
For a specific example, look at async.parallel: https://github.com/caolan/async
Here's a very simple version (which I admit to still using occasionally)
RequestHandler = {
numberOfRequests: 3
makeRequests: function() {
var __this = this;
a(function() {
// Parse Data
__this.completeRequest();
})
b(function() {
// Parse Data
__this.completeRequest();
})
c(function() {
// Parse Data
__this.completeRequest();
})
},
completeRequest: function() {
this.numberOfRequests--;
if(this.numberOfRequests == 0 ) this.doDone();
}
doDone: function() {
// We now have all data, do what we will.
}
}
RequestHandler.makeRequests();(as always with Node, you can achieve the same benefit using nonblocking I/O frameworks for other languages, e.g. Twisted or gevent in Python, EventMachine in Ruby, etc)
Services are software built to handle requests. You can build both consumers and providers of requests using Node.JS.
Tried to at least give you some small idea to get you started. The explanation is NOT perfect by any means. In fact I think I may have down played it. Either way, read the docs.
I enjoy Node.JS alot.