caster

Multicast Service Discovery

node-caster

Multicast Service Discovery

Caster provides a multicast server with middleware support. json, hash, and crypto middleware is included.

Node implements service discovery on top of Caster.

npm install caster
Stability: 2 - Unstable
var caster = require('caster');
 
var server = caster.create();
 
server.use(
    caster.middleware.json(),
    caster.middleware.hash({secret: 'mySecretHashPrefix'}),
    caster.middleware.crypto({key: 'mySecretKey'})
);
 
server.on('message', function(messageremote) {
  console.log(message, remote);
});
 
server.bind(function(err) {
  if (err) {
    console.log(err);
  } else {
    server.send({hello: 'world'}, function(errbytes) {
      console.log(err, bytes);
      server.close();
    });
  }
});

Creates 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: 10101,
  loopback: true,
  ttl: 64
}

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

Adds middleware.

server.use(
    caster.middleware.json(),
    caster.middleware.hash(),
    caster.middleware.crypto()
);

The middleware execution order, resulting from the above example, would be json.tx -> hash.tx -> crypto.tx for outgoing, and crypto.rx -> hash.rx -> json.rx for incoming messages.

Sends a message to all listening multicast servers.

message is a Buffer.

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

Enables the multicast udp socket.

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

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

opt_callback is an optional function().

function(message, remote)

message is a Buffer.

remote is an object with address and port.

function(err)

An example middleware object:

{
  name: 'json',
  txfunction(messagenext) {...},
  rxfunction(messageremotenext) {...}
}

Creates a new crypto middleware to encrypt outgoing & decrypt incoming messages.

opt_options is an optional Object.

{
  key: '09dfDim3ZqzXvfkun6',
  algorithm: 'aes256'
}

Creates a new hash middleware to hash outgoing & verify incoming messages.

opt_options is an optional Object.

{
  secret: '9ve2cND;d3"Vs',
  algorithm: 'sha1'
}

Creates a new json middleware to allow usage of a stringifyable object instead of a Buffer as message.

Stability: 2 - Unstable
var node = require('caster').createNode({
  meta: {
    type: 'http',
    port: 8080
  }
});
 
node.on('up', function(remote) {
  console.log('node up:', remote.id, remote.meta);
});
 
node.on('down', function(remote) {
  console.log('node down:', remote.id, remote.meta);
});
 
node.on('message', function(messageremote) {
  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);
  }
});

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

opt_options is an optional Object.

{
  id: null, // your node's id, randomized if not set 
  meta: null, // your node's metadata that is transmitted with every heartbeat 
  expose: true, // when false, no heartbeat messages are send 
  heartbeat: 1000, // heartbeat interval in ms 
  timeout: 2000, // timeout until a remote node is declared down 
 
  // caster options 
  multicast: '224.0.0.42',
  port: 10101,
  ttl: 64,
 
  // caster middleware options 
  hash: true, // enable message hashing 
  hashSecret: null, // hash secret 
  hashAlgorithm: 'sha1', // hash algorithm 
  crypto: false, // enable message encryption 
  cryptoKey: null, // encryption key 
  cryptoAlgorithm: 'aes256' // encryption algorithm 
}

Starts up the node.

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

Shuts down the node.

opt_callback is an optional function().

Sends 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.

An object with all currently seen nodes.

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

function(message, remote)

message is the received & parsed json object.

remote is the object stored in node.nodes.

function(Error)

Emitted when a remote node is discovered.

function(remote)

remote is the object stored in node.nodes.

Emitted when a remote node disappeared.

function(remote)

remote is the object just removed from node.nodes.