Node.js modules you should know about: dnode


Hey everyone! I am starting a new article series called node.js modules you should know about. I have been using node for over 2 years now and I built Browserling startup using node so I know just about everything about it. I also have written about 20 node.js modules myself (see my github).

In this series I will go through a few dozen of node.js modules, give examples and explain where it’s useful.

The first module in the series is dnode. Dnode is freestyle rpc library and it’s written by James Halliday (SubStack) ― co-founder of Browserling and Testling.

Here is what it is. This is the server.js:

var dnode = require('dnode');

var server = dnode({
    mul : function (n, m, cb) { cb(n * m) }
});
server.listen(5050);

And here is the client.js:

var dnode = require('dnode');

dnode.connect(5050, function (remote) {
    remote.mul(10, 20, function (n) {
        console.log('10 * 20 = ' + n);
    });
});

Now when you run client.js, you get the output:

$ node client.js
200

See what it did? It called the mul function at server side from the client side and passed it arguments 10 and 20. They got multiplied at server side and the result got sent back to the client by calling cb.

It’s important to stress that no code was passed along, all this happened purely through references. You can see the implementation dnode protocol in dnode-protocol github repo.

Here is a more complex example, where client calls server, which calls client again, which passes the result back to server, which then calls client and prints the result.

server.js:

var dnode = require('dnode');

var server = dnode(function (client) {
    this.calculate = function (n, m, cb) {
        client.div(n*m, function (res) {
            cb(res+1)
        });
    }
});
server.listen(5050);

client.js:

var dnode = require('dnode');

var client = dnode({
    div : function (n, cb) {
       cb(n/5);
    }
});

client.connect(5050, function (remote) {
    remote.calculate(10, 20, function (n) {
        console.log('the result is ' + n);
    });
});

When you run the client, you’ll get result 41. Here is what happens. First you connect to dnode server at port 5050. Once you’re connected, dnode client calls calculate function on server side and passes it arguments 10 and 20 and a callback function that prints the result. Now when the server receives the arguments 10 and 20, it multiplies them together and calls the client’s div function, that divides the result by 5. The result is returned back to the server and it adds 1 to it and calls the original callback that prints the result.

We use dnode everywhere at Browserling. Every service is a dnode server and they are all interconnected. For example, the authentication is a dnode server. We can bring it down and update, while the rest of the site is up. Really awesome.

You can install dnode through npm:

npm install dnode

And since dnode has a well defined protocol, you can implement it in any language! Here are dnode implementations in Perl, Ruby, PHP, Java.

Enjoy this rapping turtle!

If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we’ll set it up!

Copy from http://www.catonmat.net/blog/nodejs-modules-dnode/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s