Skip to content

Instantly share code, notes, and snippets.

@stash
Created September 30, 2011 21:09
Show Gist options
  • Save stash/1254978 to your computer and use it in GitHub Desktop.
Save stash/1254978 to your computer and use it in GitHub Desktop.
Streaming data pubsub - node.js + redis
var redis = require('redis');
var pubcli = redis.createClient(6380, '127.0.0.1');
var subcli = redis.createClient(6380, '127.0.0.1');
var datacli = redis.createClient(6380, '127.0.0.1', {return_buffers:true});
var have = 0;
datacli.lrange('data.theresource', 0, -1, function(err,chunks) {
console.log("initial data!",chunks);
have = chunks.length;
});
subcli.on("subscribe", function(channel, count) {
console.log("subscribe!", channel, count);
});
subcli.on("message", function(channel, message) {
var need = parseInt(message);
console.log("list is now "+need+" items long");
if (have >= need) return; // needed in case we fetch the list before the publish about it
datacli.lrange('data.theresource',have,need-1,function(err,chunks) {
console.log("data!",chunks);
});
have = need;
});
subcli.subscribe("pubsub.theresource");
setTimeout(function() {
var binary = new Buffer(4);
binary[0] = 0;
binary[1] = 1;
binary[2] = 2;
binary[3] = 3;
pubcli.rpush("data.theresource", binary, function(err,newSize) {
console.log("pushed",arguments);
pubcli.publish("pubsub.theresource",""+newSize);
});
},800);
setTimeout(function() {
var binary = new Buffer(0);
pubcli.rpush("data.theresource", binary, function(err,newSize) {
console.log("pushed",arguments);
pubcli.publish("pubsub.theresource",""+newSize);
});
},1600);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment