This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

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!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->

Comments

Alexander Sicular Permalink
December 01, 2011, 22:46

So how does this work with the asynchronous callback soup. If you are remoting out over a net process isn't your client going to move processing forward before results return? Do you have to go to a full event/emit code style to successfully work with dnode? does all you code need to live inside client.connect?

Are there accessible dnode based projects on github you would recommend people check out?

Thanks you!

December 02, 2011, 15:37

Yes, event emitters work over dnode. See this article: http://substack.net/posts/9bac3e

See the examples folder in dnode for more examples. There are a lot of projects that use dnode, see hook.io for example.

stefs Permalink
December 07, 2011, 16:23

this is pretty nice, but ... it takes (js) code as a parameter! i know that's not a problem at all - in javascript. but how do the dnode implementations in the other languages deal with that?

macy Permalink
October 29, 2013, 08:22

It's nice that they have thought about that kind of way.
See hermes shoulder birkin.

Sawyer Permalink
March 03, 2014, 20:54

Its very nice information you have to share with us and i really impress due to it and sharing essay writers services for students online.

Chad Permalink
April 12, 2014, 15:11

I was just to discover something like this :) http://wingtsunrandwick.com .

November 26, 2014, 18:48

Terima kasih infonya gan :))

http://goo.gl/YyhwYt

November 26, 2014, 18:53

Artikel yang sangat menarik =))

http://goo.gl/lNMX3D

Sonakshi Permalink
November 29, 2014, 17:22

dnode is a freestyle rcp. sounds good thanks for sharing with us and find all basic bank details of State Bank of India of Anantapur dist in this blog.

December 05, 2014, 20:10

Nice and very informative post.tnx
new year wishes messages
new year wishes messages for facebook
new year wishes messages for family and friends.A must read list for this new year.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "lcd_227": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements