Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Multicast Service Discovery for node.js
tag: v0.0.7

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
.jshintrc
LICENSE
Makefile
README.markdown
index.js
package.json

README.markdown

node-caster

A collection of multicast servers

install

npm install caster

api

Caster - a multicast server

Stability: 2 Unstable

example

var caster = require('caster');

var server = caster.create({
  multicast: '224.0.0.54',
  port: 41234
});

server.use(caster.middleware.json());

server.on('message', function(message, remote) {
  console.log(message, remote);
});

server.bind(function(err) {
  if (err) {
    console.log(err);
  } else {
    server.send({hello: 'world'}, function(err, bytes) {
      console.log(err, bytes);
      server.close();
    });
  }
});

caster.create(options)

Create a new Caster server. Caster is an EventEmitter.

opt_options is an optional Object.

{
  multicast: '224.0.0.42',
  address: '0.0.0.0',
  port: null,
  loopback: true,
  ttl: 64
}

Note: All nodes have to use the same multicast address & port to be able to communicate.

server.use(middleware)

Add middleware.

server.send(message, opt_callback)

Send a message to other multicast nodes.

message is a Buffer.

opt_callback is an optional function(err, bytes). A possible error is emitted when no callback is set.

server.bind(opt_callback)

Enable the multicast udp socket.

opt_callback is an optional function(err). A possible error is emitted when no callback is set.

server.close(opt_callback)

Close the socket. Allows to bind() again with the same caster object.

opt_callback is an optional function().

Event: 'message'

function(message, remote)

message is a Buffer.

remote is an object and contains information on the remote node.

Event: 'error'

function(err)

middleware

hash(opt_options)

Hash & verify messages.

opt_options is an optional Object.

{
  secret: '9ve2cND;d3"Vs'
}

json()

Allows to use a stringify-able object instead of a Buffer as message.

Node - a network discovery server

Stability: 2 Unstable

example

var node = require('caster').createNode({
  multicast: '224.0.0.54',
  port: 41234
});

node.on('up', function(remote) {
  console.log('up:', remote.id);
});

node.on('down', function(remote) {
  console.log('down:', remote.id);
});

node.on('message', function(message, remote) {
  console.log(remote.id + ':', message);
});

node.up(function(err) {
  if (err) {
    console.log(err);
  } else {
    console.log('caster node running on ' +
        node.multicast + ':' + node.port);
  }
});

caster.createNode(opt_options)

Create a new Node object. Node is an EventEmitter and uses Caster.

opt_options is an optional Object.

{
  id: 'your_unique_node_id', // randomized if not set
  heartbeat: 1000, // heartbeat interval in ms
  timeout: 2000, // timeout until a remote node is declared down
  expose: true, // when false, no heartbeats are send
  // and all options of Caster
  multicast: '224.0.0.42',
  address: '0.0.0.0',
  port: null, // randomized if not set
  loopback: true,
  ttl: 64
}

node.up(opt_callback)

Fire up the node.

opt_callback is an optional function(err). A possible error is emitted when no callback is set.

node.down(opt_callback)

Shut down the node.

opt_callback is an optional function().

node.send(message, opt_callback)

Send a message to other multicast nodes.

message is a String, Number or Object.

opt_callback is an optional function(err, bytes). A possible error is emitted when no callback is set.

node.nodes

An object with all currently seen nodes.

{
  node857631295: {
    id: 'node857631295',
    address: '10.0.0.23',
    lastSeen: 1340351517064
  },
  ...
}

Event: 'message'

function(message, remote)

message is the received & parsed json object.

remote is the object stored in node.nodes.

Event: 'error'

function(Error)

Event: 'up'

Emitted when a remote node is discovered.

function(remote)

remote is the object stored in node.nodes.

Event: 'down'

Emitted when a remote node disapeared.

function(remote)

remote is the object just removed from node.nodes.

Something went wrong with that request. Please try again.