Send Message From Server To Client With Dnode
A couple of month ago I discovered nowjs and dnode and finally used nowjs (and https://github.com/Flotype/nowclient) for client / server bidirectional communication. nowclient en
Solution 1:
dnode uses a symmetric protocol so either side can define functions that the opposing side can call. There are 2 basic approaches you can take.
The first way is to define a register function on the server side and to pass in a callback from the client.
server:
var dnode = require('dnode');
dnode(function (remote, conn) {
this.register = function (cb) {
// now just call `cb` whenever you like!// you can call cb() with whichever arguments you like,// including other callbacks!setTimeout(function () {
cb(55);
}, 1337);
};
}).listen(5000)
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 could directly call the client from the server in a symmetric fashion once the method exchange is complete:
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);
Post a Comment for "Send Message From Server To Client With Dnode"