dnode uses a symmetric protocol so that both sides can define functions that the opposite side can call. There are two main approaches you can take.
The first way is to define the registration function on the server side and pass a callback from the client.
server:
var dnode = require('dnode'); dnode(function (remote, conn) { this.register = function (cb) {
client:
var dnode = require('dnode'); dnode.connect('localhost', 5000, function (remote, conn) { remote.register(function (x) { console.log('the server called me back with x=' + x); }); });
or instead, you can directly call the client from the server in a symmetric way after the exchange of methods is completed:
server:
var dnode = require('dnode'); dnode(function (remote, conn) { conn.on('ready', function () { remote.foo(55); }); }).listen(5000);
client:
var dnode = require('dnode'); dnode(function (remote, conn) { this.foo = function (n) { console.log('the server called me back with n=' + n); }; }).connect('localhost', 5000);
source share