littlehook

A lan-wide EventEmitter2 implementation supporting complete decentralization, auto-discovery and request-response emulation.

npm install littlehook
1 downloads in the last week
4 downloads in the last month

littlehook

littlehook is a lan-wide, distributed EventEmitter2 implementation supporting complete decentralization, auto-discovery & request-response emulation, powered by MDNS.

What does it do

Inspired by Hook.io and Tinyhook, littlehook provides lightweight, namespaced and fully-decentralized eventing in a "pure p2p" fashion. Littlehook is a distributed implementation of EventEmitter2.

Each hook uses MDNS in a store-sub fashion to discover its peers, monitor their subscriptions and publish his own subscriptions. NsSocket and EventEmitter2 are used to push events to the appropriate listener hooks. Each hook is free to fail without compromising the rest of its peers or the network (no meshes, no trees). Request-response emulation is provided.

Status

Pre-alpha and don't even think about using this in production - otherwise working pretty well.

This is an updated version of the original package, one of my very first coding experiments with Node.js. I decided to clean it up a bit, improve it in some areas and put it back online after the original, unmaintained version that I thought I had removed long ago from both NPM and GitHub surprisingly sparked some interest around Jan 2013.

At the moment, I'm not maintaining and/or improving this package. Should it keep gathering attention, though, I'll think about a serious makeover.

To-Do

  • Socket timeout and takedown
  • P2P event flooding for large networks

Usage & API

var Hook = require('littlehook'); 

var a = new Hook({ name: 'a', port: 9999 });
var b = new Hook({ name: 'b', port: 9998 });

// Matches events of type 'event::type' sent by 
// any hook in the same MDNS area
a.on(['*', 'event', 'type'], function(data) {
    console.log('data: ' + data);
    console.log('sender: ' + this.event[0]);
});

// This does the same as the above w/ events
// specified as strings instead of arrays
a.on('*::event::type', function(data) {
    console.log('data: ' + data);
    console.log('sender: ' + this.event.split('::')[0]);
});

// When hook 'a' comes online, emits event of
// type 'event::type' - the resulting event will
// be namespaced as 'b::event::type'
b.on(['a', 'up'], function() {
    b.emit(['event', 'type'], { some: 'data' });
});

a.start();
b.start();

Each hook is an EventEmitter2 instance, see the relative API specs.

Events are namespaced as in Tinyhook and Hook.io.

var hook = new Hook({name: 'johnny', port: 9999});
hook.emit('hello'); // Event emitted: ['johnny', 'hello']

The delimiter used is always '::' (see EventEmitter2's specs):

'hookName::event::type'   <->   ['hookName', 'event', 'type'] 

Each hook emits the following events:

['hookName', 'up']     -> Hook 'hookName' came up
['hookName', 'down']   -> Hook 'hookName' went down
['hookName', 'update'] -> Hook 'hookName' updated its subscriptions

In addition, each hook provides the following request-response methods:

// Type-based response mechanism
hook.respond('senderHook::request::type', handler(reqData, reply){
    var requestEvent = this.event;
    reply('response::type', 'response data');
});

// Make a request
hook.request(
    'targetHook::request::type', 
    'request data',
    function(responseData){
        var responseEvent = this.event;
        // Do smthg for each request
    },
    1000, // Timeout interval
    function(){
        // Do smthg after the timeout
    }
);

// Stop
hook.stopResponding('request::type');
npm loves you