Skip to content

Zero Configuration MultiCast

Jared Rice Sr edited this page Jul 1, 2018 · 2 revisions

@distdns/zero

DWebZero is a zero configuration (zeroconf, mdns or dns-sd) service revelation module written completely in Javascript. Unlike some other tools (https://github.com/agnat/node_mdns) it does not require the installation of Apple's Bonjour SDK. It's available through npm:

npm install @distdns/zero

What problem does it solve?

DWeb Zero allows your servers/programs to revelate each other without having to talk to a central server and without the use of any static configuration as long as they are connected to the same local network.

Usage

First create a dWebZero instance:

vardWebZero=require('@distdns/zero');varapps=dWebZero();

Now let's add a service to the app repository:

apps.put({name:'greetings-martian',// required - the name of the servicehost:'example.com',// defaults to the network ip of the machineport: 8080// we are listening on port 8080.});

If you put multiple services with the same name DWeb Zero will load balance them for you by choosing a random service. Now spin up another node process and dWebZero will automatically distribute information about this service:

// in another processvardWebZero=require('@distdns/zero');varapps=dWebZero();apps.once('up',function(name,service){// up fires everytime some service joinsconsole.log(apps.get(name));// should print out the joining service, e.g. hello-world});

Additionally there is a down event which fires when a services leaves the repository - it's that easy!

Options

Per default DWeb Zero will revelate all services running on a network using UDP multicast. When developing it can often be very useful to disable this. To do so either provide multicast: false or set your NODE_ENV=development environment variable

varapps=dWebZero({multicast: false// disables network multicast,monitor: true// fork a monitor for faster failure detection,heartbeat: 2*60*1000// set the service heartbeat interval (defaults to 2min)});

or using development mode from the shell

$ NODE_ENV=development node my-zero-app.js # also disables network multicast

Example

Let's create an HTTP service. Try to run the program below on different machines in the same network:

varhttp=require('http');vardWebZero=require('@distdns/zero');varapps=dWebZero();varserver=http.createServer(function(req,res){if(req.url!=='/'){res.writeHead(404);res.end();return;}res.end('greetings-http is available at http://'+apps.get('greetings-http').address);});server.listen(0,function(){varport=server.address().port;// let's find out which port we binded toapps.put({name: 'greetings-http',port: port});console.log('visit: http://localhost:'+port);});

Clone this wiki locally