Skip to content

Instantly share code, notes, and snippets.

@egm0121
Created January 31, 2013 13:48
Show Gist options
  • Save egm0121/4682991 to your computer and use it in GitHub Desktop.
Save egm0121/4682991 to your computer and use it in GitHub Desktop.
real time user tracking socket.io + Redis
<script src="socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
});
socket.emit('setinfo',{'bella merda':navigator.userAgent})
</script>
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, redis = require("redis"),
toJson = JSON.stringify,
redisConn = redis.createClient(null,'10.64.98.222');
app.listen(81);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
redisConn.incr("node_count_test");
redisConn.decr("node_count_test");
io.sockets.on('connection', function (socket) {
redisConn.incr("node_count_test");
socket.on('setinfo', function(info){redisConn.lpush("node_list_test",toJson(info));socket.set('ts', info, function (){});});
socket.on('disconnect',function(){
socket.get('ts',function(err,i){
console.log(toJson(i));
redisConn.lrem("node_list_test",1,toJson(i));
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment