Skip to content

Instantly share code, notes, and snippets.

@kpnemo
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save kpnemo/0e478f541e0b2b54a250 to your computer and use it in GitHub Desktop.

Select an option

Save kpnemo/0e478f541e0b2b54a250 to your computer and use it in GitHub Desktop.
var events = require('events');
var Events = new events.EventEmitter;
var redis = require('redis');
var store = redis.createClient(6379, 'localhost');
var pub = redis.createClient(6379, 'localhost');
var sub = redis.createClient(6379, 'localhost');
sub.setMaxListeners(0);
sub.subscribed = false;
sub.listening = false;
Events.on('message', function(num){
console.log('EVENT:' + num);
if(!sub.subscribed){
sub.subscribed = true;
sub.on('subscribe', function(channel){
console.log('SUB:SUBSCRIBED TO CH:' + channel);
});
}
if(!sub.listening){
sub.listening = true;
sub.on('message', function(chanel, message){
console.log('ONMESSAGE FROM CH: ' + chanel + ' with message ' + message);
});
}
sub.subscribe('msg:' + num);
});
for(i=0;i<=12;i++){
console.log('JUST COUNT:' + i);
var publishOnSubscribe = function(channel, msg){
console.log('PUBLISHING TO CH:' + channel + ' message: ' + msg);
pub.publish(channel, msg);
};
Events.on('subscribed:'+i, publishOnSubscribe);
Events.emit('message', i);
var publish = function(chunnel, msg){
setTimeout(function(){
publishOnSubscribe(chunnel, msg);
}, 1000);
}
publish('msg:' + i, i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment