Nodejitsu

Save time managing and deploying your node.js app. Code faster with jitsu and npm

Distribute Node.js Apps with Hook.io

About the author

Name
Location
Worldwide
nodejitsu nodejitsu

This is Part two of a four part series leading up to Node Knockout

Nodejitsu is very happy to announce the latest stable version of hook.io. This release marks a combined effort from the Nodejitsu team working across six time-zones and four countries. But what does this mean for the application developer and what is hook.io anyway? Hook.io is a full-featured I/O framework for node.js that enables a simple way to distribute your application across multiple node.js processes using the new EventEmitter2 API and leveraging the power of Crash-only software.

In a nutshell, hook.io allows you to split your node.js program into multiple worker processes, that will be automatically restarted upon any user-generated faults, allowing you to increase performance and fault-tolerance. Interested in learning more? Read on!


Wait a second, EventEmitter2?

No, you didn't read it wrong; Nodejitsu has been working on EventEmitter2 for the last couple of months and there has been positive feedback about bringing it into node.js core. EventEmitter2 is a node.js event emitter implementation with namespaces, wildcards, TTL and browser support. Even if it is not brought into node.js core, it will continued to have the full support and backing of Nodejitsu; now and forever.

Lets take a look at how EventEmitter2 works:

var EventEmitter = require('eventemitter2').EventEmitter;

var emitter = new EventEmitter({ wildcard: true });

//
// Existing node.js non-namespaced events continue to work as normal
//
emitter.on('event', function (line) {  
  console.log('event: ' + line);
});

emitter.emit('event', 'has fired');

//
// EventEmitter2 also adds support for wildcard events, using `*`
//
emitter.on('wild.*', function (line) {  
  console.log('wild.*: ' + line);
});

emitter.on('wild.specific', function (line) {  
  console.log('wildcard.specific: ' + line);
});

emitter.emit('wild.catchall', 'Captured by `wild.*`');  
emitter.emit('wild.specific', 'Captured by `wild.*` and `wild.specific`');  

If you don't pass in { wildcard: true } when creating a new Emitter, then EventEmitter2 works exactly as it does now in node.js core. In addition to all the new features, EventEmitter2 will also work seamlessly in the browser, so it can be used in conjunction with modules like browserify.


A simple Hook.io program

Hook.io programs in general are simple and collaborative, so don't be surprised if you end up writing very small programs that only respond to a couple of events. This is the nature of hook.io, as I will discuss later in the article. So what does such a simple program look like?

var Hook = require('hook.io').Hook,  
    util = require('util');

var Helloworld = exports.Helloworld = function (options) {  
  var self = this;

  Hook.call(this, options);

  this.on('hook::ready', function () {
    //
    // This is just to simulate I/O, don't use timers...
    //
    setInterval(function () {
      self.emit('hello', 'Hello, I am ' + self.name);
    }, 1000);
  });
};

//
// Inherit from `hookio.Hook`
//
util.inherits(Helloworld, Hook);  

Of course, this hook is a contrived example. If you're looking for some real-world examples checkout hook.io-webhook or hook.io-request. You could look at this hook as a simple heartbeat, every second this hook will emit the hello event with the auto-discovered hook name.

Lets assume that you have created a parent hook which has spawned two instances of the helloworld hook. This parent hook acts as an intermediary between the two children hooks and rebroadcasts their messages to all peers. By emitting hello in the hook code, a hook-id::hello message is sent to the parent hook who in turn rebroadcasts it to all peers. As a result, any peer hook wishing to receive this message must listen for *::hello. Lets take look at a simple network diagram for this message:




The hook.io ecosystem

The real power of the hook.io project is that there are a ton of logic-specific hooks for you to build programs on. Just take a look at the hook.io Github page. Here's a quick sample of hooks:



Roadmap and beyond

Hook.io is in some ways a established project and in others a young project. Marak Squires gave a talk on Hook.io at JSConf 2010, but most of that code has been completely rewritten for the 0.5.x and 0.6.x versions to take full advantage of EventEmitter2, dnode, and Forever.

Hook.io is now deeply integrated into haibu-carapace, the event-based application host used by haibu. It is also leveraged by other important projects at Nodejitsu such as kohai and station. On the horizon you can look forward to more examples, tutorials, and network transports for you to build hook programs out in "hook-land". Stay tuned!