forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add redis online user activity tracking example
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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'); |