Skip to content

Instantly share code, notes, and snippets.

@lexer
Created May 8, 2011 19:08
Show Gist options
  • Save lexer/961604 to your computer and use it in GitHub Desktop.
Save lexer/961604 to your computer and use it in GitHub Desktop.
Node pubsub server based on socket.io and redis
var client = new Pubsub({
port: 8000
});
client.connect();
client.on("connect", function() {
client.subscribe("trololo", function(data){
console.log('Received a message from the server: ' + data);
});
});
var io = sio.listen(app);
io.on('connection', function(socket){
var sub = redis.createClient();
socket.on('message', function (msg) {
if (msg.action === "subscribe") {
console.log("Subscribe on " + msg.channel);
sub.subscribe(msg.channel);
}
if (msg.action === "unsubscribe") {
console.log("Unsubscribe from" + msg.channel);
sub.unsubscribe(msg.channel);
}
});
socket.on('disconnect', function () {
sub.quit();
});
sub.on("message", function (channel, message) {
console.log(channel +": " + message);
socket.send({
channel: channel,
data: message
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment