wankdanker / node-discover

Automatic and decentralized discovery and monitoring of nodejs instances with built in support for a variable number of master processes, service advertising and channel messaging.
229 stars 59 forks source link

Using Node-Discover #2

Closed Firefly7 closed 11 years ago

Firefly7 commented 11 years ago

Hi,

I have started using node.js recently and got fascinated by its speed and processing. I was just trying all the modules I found. But can anyone tell me how to use Node-discover protocol to discover nodes. I have tried many ways (which might be wrong), resulted in null. How to implement node-discover in-order to discover running node.js process?

Please help me.

wankdanker commented 11 years ago

Hey @Firefly7,

In its most simple form, every process that you want to be discover-able must include at least the following code:

var Discover = require('node-discover');
var d = new Discover();

In any process which you want to discover other processes, you need to add some event handlers, like this:

var Discover = require('node-discover');
var d = new Discover();

d.on("added", function (obj) {
    console.log("A new node has been added.");
});

d.on("removed", function (obj) {
    console.log("A node has been removed.");
});

In its most simple form as described above, it uses UDP broadcasts to advertise each node. Once new nodes are discovered, the event handlers are then called and passed a node object, (see https://github.com/wankdanker/node-discover#node-object). Since we are using a totally basic and bare-bones example here, we won't get too much information along with the node object because our discover-able processes are not advertising anything. But we will have the ip address, host name and some other basic information about the host running the process.

Let me know if you have any more questions and I'll do my best to help.

Dan

Firefly7 commented 11 years ago

I am sorry, this might be a basic question, I have 3 process, just simple node.js servers running on different port address in same machine. I've added the above code in each of them for discovering other processes. when I run them, they are just getting executed without any result for discovering. Do i need to add any other event handlers for the nodes to get discovered.?

wankdanker commented 11 years ago

On my system if I create a file test.js which contains the following code:

var Discover = require('node-discover');
var d = new Discover();

d.on("added", function (obj) {
    console.log("A new node has been added.");
    console.log(obj);
});

d.on("removed", function (obj) {
    console.log("A node has been removed.");
    console.log(obj);
});

Then from within the directory that contains test.js execute npm install node-discover. Then in three separate terminals execute node test.js. I then see the events displayed in each terminal.

What happens when you try this? What operating system are you on?

Thanks,

Dan

Firefly7 commented 11 years ago

I was also trying same as you mentioned, but when I run the test.js in all the three terminals, I don't see any events displayed in the terminals. I am running linux flavor [ubuntu 13.04] [with a Kubuntu desktop installed] on a virtual box, Host system with windows 7. and node.js version v0.10.10. I am attaching my screen shot of the terminals, please have a look into that.

Thank you,

wankdanker commented 11 years ago

I am too on Kubuntu 13.04, node v0.10.10. It is not a virtual machine though.

I'm curious what your ifconfig looks like. It may be binding to a wrong address or have a weird broadcast address set up.

Firefly7 commented 11 years ago

Yes, got it. as you said It has got weird broadcast address and I have changed the broadcast address in lib/network.js to the same shown in ifconfig. It started working.

Thanks a lot for the help.

wankdanker commented 11 years ago

Cool. Glad you got it working. Just so you are aware, you don't need to modify lib/network to change the broadcast address. You can specify the broadcast address on the options object passed to the Discover constructor. See the options here: https://github.com/wankdanker/node-discover#constructor

For example you could do...

var Discover = require('node-discover');
var d = new Discover({ broadcast : '255.255.255.248' }); // or whatever your broadcast address is

That setting will carry all the way through to the network layer.

Hope this helps.

Dan

Firefly7 commented 11 years ago

Yes, that works too. Thanks for the help.