digger-radio

Pub/Sub client for digger-client

digger-radio

Pub/Sub client for digger-client

$ npm install digger-radio --save

The radio is a pub/sub wrapper to a backend server infrastructure.

It 'subscribes' by sending the subscription key and registering a local function as a callback for that key.

var Radio = require('digger-radio');
var radio = new Radio();
 
radio.listen('apples', function(payload){
  // we heard 10! 
})
 
radio.talk('apples', 10);
 

Wildcards are supported:

var radio = new Radio();
 
radio.listen('apples.*', function(payload){
  // we heard 10!
})
 
radio.talk('apples.pears', 10);

If you pass a base path to the constructor - it is transparently prepended to the subscription key.

var radio = new Radio('my.section.with.dots.');
 
radio.listen('apples.*', function(payload){
  // the actual path written:
  // my.section.with.dots.apples.pears
})
 
 
radio.talk('apples.pears', 10);

To cancel a listening function:

var radio = new Radio('apples.');
 
var count = 0;
var handler = function(){
  count++;
  if(count>1){
    throw new Error('should not happen twice');
  }
}
 
radio.listen('pears', handler);
radio.talk('pears', 10);
radio.cancel('pears', handler);
radio.talk('pears', 10);
 

The radio does not try to handle getting messages sent to the pub/sub server.

Instead - it emits the following events - allowing whatever transport layer (WebSockets, ZeroMQ) to handle the packet:

This is used to send a subscription key to the pub/sub server.

This is effectively saying 'tell me about these messages'.

An example of the digger-sockets transport layer:

 
var radio = new Radio();
 
radio.on('talk', function(channelpayload){
    socket.send(JSON.stringify({
        type:'radio:talk',
        data:{
            channel:channel,
            payload:payload
        }
    }))
})
 
radio.on('listen', function(channel){
    socket.send(JSON.stringify({
        type:'radio:listen',
        data:channel
    }))
})
 
radio.on('cancel', function(channelpayload){
    socket.send(JSON.stringify({
        type:'radio:cancel',
        data:channel
    }))
})
 

MIT