Created
September 30, 2011 21:09
-
-
Save stash/1254978 to your computer and use it in GitHub Desktop.
Streaming data pubsub - node.js + redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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