Skip to content

Instantly share code, notes, and snippets.

@temsa
Created September 22, 2011 08:43
Show Gist options
  • Save temsa/1234359 to your computer and use it in GitHub Desktop.
Save temsa/1234359 to your computer and use it in GitHub Desktop.
Simple example for basic Hook.emit syntax using group feature
/*
* Simple example for basic Hook.emit syntax using group feature
*/
var Hook = require('../../../lib/hookio').Hook;
var server = new Hook({
name: "server-hook"
});
var client1 = new Hook({
name: "client1-hook"
});
var client2 = new Hook({
name: "client2-hook"
});
server.on('*::hello', function(data){
console.log('server says hello'.green.bold, data.content.rainbow.bold)
})
client1.on('*::hello', function(data){
console.log('client1 says hello'.blue.bold, data.content.rainbow.bold)
})
client2.on('*::hello', function(data){
console.log('client2 says hello'.yellow.bold, data.content.rainbow.bold)
})
server.on('hook::ready', function(){
client1.start();
//TODO: use async
client1.on('hook::ready', function(){
client2.start();
client2.on('hook::ready', function(){
client2.emit("query", {event:"*::hello"}, function(hooks) {
var hook = hook[Math.floor(Math.random() * (max + 1)];
client2.emit([hook.name,'hello'].join('::'), {content: 'world'});
}
});
});
});
server.start();
/*
* Simple example for basic Hook.emit syntax using group feature
*/
var Hook = require('../../../lib/hookio').Hook;
var server = new Hook({
name: "server-hook"
});
var client1 = new Hook({
name: "client1-hook"
});
var client2 = new Hook({
name: "client2-hook"
});
server.on('*::hello', function(data){
console.log('server says hello'.green.bold, data.content.rainbow.bold, "but shouldn't :(".red.bold)
})
client1.on('*::hello', function(data){
console.log('client1 says hello'.blue.bold, data.content.rainbow.bold, "and should ! :)".green.bold)
})
client2.on('*::hello', function(data){
console.log('client2 says hello'.yellow.bold, data.content.rainbow.bold, "but shouldn't :(".red.bold)
})
server.on('hook::ready', function(){
client1.start();
//TODO: use async
client1.on('hook::ready', function(){
client2.start();
client2.on('hook::ready', function(){
client2.emit('client1-hook::hello', {content: 'world'});
});
});
});
server.start();
var async = require('async'),
_ = require('underscore'),
Hook = require('hook.io').Hook,
assert = require('assert');
async.forEach(_.range(10), function(it, callback) {
var hook = new Hook({ debug: true });
hook.start(callback);
}, function(err) {
assert.ifError(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment