Skip to content

Instantly share code, notes, and snippets.

@vanics
Forked from mort/kandypot-node.js
Created January 16, 2018 19:45
Show Gist options
  • Save vanics/f6083e38d5fd460df30493c7736b538e to your computer and use it in GitHub Desktop.
Save vanics/f6083e38d5fd460df30493c7736b538e to your computer and use it in GitHub Desktop.
Node.js + Redis pubsub + Websockets for kandypot.com.
// Really trivial. On new writes to the redis db, we push messages to a websocket channel
var sys = require("sys"), redis = require('./lib/redis-client'), ws = require("./lib/ws");
var pubsub = redis.createClient();
var clients = new Array();
ws.createServer(function (websocket) {
websocket.addListener("connect", function (resource) {
sys.debug("connect: " + resource);
client_id = resource.match(/apps\/([A-Za-z]+)\/timeline$/)[1];
sys.debug("connecting to app: "+client_id);
clients[client_id] = websocket;
}).addListener("data", function (data) {
sys.debug(data);
}).addListener("close", function () {
sys.debug("close");
});
}).listen(8080);
pubsub.stream.addListener('connect', function(){
sys.debug('Connecting ..');
pubsub.subscribeTo('apps:*:timeline', function(channel,data){
sys.debug(data);
var cid = channel.toString().split(':')[1];
sys.debug("Cid: "+cid);
if (clients[cid]) {
clients[cid].write(data);
}
else {
sys.debug("Client "+clients[cid]+" is not connected");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment