Skip to content

Instantly share code, notes, and snippets.

@UserAd
Created August 6, 2010 14:11
Show Gist options
  • Save UserAd/511363 to your computer and use it in GitHub Desktop.
Save UserAd/511363 to your computer and use it in GitHub Desktop.
var sys = require('sys');
var redis = require('./lib/redis-client');
var ws = require('./lib/ws');
var clients = [];
var pubsub = redis.createClient();
pubsub.stream.addListener('connect', function(){
sys.debug('Connected to queue');
pubsub.subscribeTo("cargoboss:auction:*:bids", function(channel, data){
var auction_id = channel.toString().split(':')[2];
if (clients[auction_id] != null)
{
for(var i=0;i<clients[auction_id].length;i++)
{
clients[auction_id][i].write(data);
}
}
});
});
ws.createServer(function(socket){
var websocket = socket;
var queue = null;
var id;
websocket.addListener('connect', function(res){
queue = res.match(/bids\/(.+)$/i)[1];
if (clients[queue] == null)
{
clients[queue] = [];
}
id = clients[queue].length;
clients[queue][id] = websocket;
}).addListener('close', function()
{
clients[queue].slice(0,id).concat(clients[queue].slice(id + 1));
});
}).listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment