Skip to content

Commit

Permalink
add redis online user activity tracking example
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 14, 2012
1 parent b96401a commit c4ad97d
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions examples/online/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

// first:
// npm install redis online

/**
* Module dependencies.
*/

var express = require('../..')
, online = require('online')
, redis = require('redis')
, db = redis.createClient();

// online

online = online(db);

// app

var app = express();

// activity tracking, in this case using
// the UA string, you would use req.user.id etc

app.use(function(req, res, next){
// fire-and-forget
online.add(req.headers['user-agent']);
next();
});

/**
* List helper.
*/

function list(ids) {
return '<ul>' + ids.map(function(id){
return '<li>' + id + '</li>';
}).join('') + '</ul>';
}

/**
* GET users online.
*/

app.get('/', function(req, res, next){
online.last(5, function(err, ids){
if (err) return next(err);
res.send('<p>Users online: ' + ids.length + '</p>' + list(ids));
});
});

app.listen(3000);
console.log('listening on port 3000');

0 comments on commit c4ad97d

Please sign in to comment.