From 5a7a8ea49346d927c68d951f04cdfc9b284e8229 Mon Sep 17 00:00:00 2001 From: Vin Brun Date: Sun, 19 Jan 2025 20:01:15 -0300 Subject: [PATCH] fix: update online example to support Redis v4+ and remove outdated dependency --- examples/online/index.js | 54 +++++++++++++++++++++++-------- examples/online/online-tracker.js | 44 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 examples/online/online-tracker.js diff --git a/examples/online/index.js b/examples/online/index.js index 0b5fdffc86..2922cb866a 100644 --- a/examples/online/index.js +++ b/examples/online/index.js @@ -4,7 +4,7 @@ // https://redis.io/ // then: -// $ npm install redis online +// $ npm install redis // $ redis-server /** @@ -12,7 +12,7 @@ */ var express = require('../..'); -var online = require('online'); +var online = require('./online-tracker'); var redis = require('redis'); var db = redis.createClient(); @@ -27,9 +27,13 @@ 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']); +app.use(async (req, res, next) => { + try { + // fire-and-forget + await online.add(req.headers['user-agent']); + } catch (err) { + next(err); + } next(); }); @@ -47,15 +51,37 @@ function list(ids) { * GET users online. */ -app.get('/', function(req, res, next){ - online.last(5, function(err, ids){ - if (err) return next(err); - res.send('

Users online: ' + ids.length + '

' + list(ids)); - }); +app.get('/', async (req, res, next) => { + try { + const activeUsers = await online.last(5); + res.send('

Users online:' + activeUsers.length + '

' + list(activeUsers)); + } catch (err) { + next(err); + } }); -/* istanbul ignore next */ -if (!module.parent) { - app.listen(3000); - console.log('Express started on port 3000'); + +/** + * Redis Initialization + */ +async function initializeRedis() { + try { + // Connect to Redis + await db.connect(); + } catch (err) { + console.error('Error initializing Redis:', err); + process.exit(1); + } } + +/** + * Start the Server + */ + +(async () => { + await initializeRedis(); // Initialize Redis before starting the server + if (!module.parent) { + app.listen(3000); + console.log('Express started on port 3000'); + } +})(); diff --git a/examples/online/online-tracker.js b/examples/online/online-tracker.js new file mode 100644 index 0000000000..17a3321a32 --- /dev/null +++ b/examples/online/online-tracker.js @@ -0,0 +1,44 @@ +'use strict'; + +const { createClient } = require('redis'); + +/** + * Factory function to create an OnlineTracker instance + * + * @param {Object} redisClient - The Redis client instance. + * @returns {Object} An object with methods for user activity tracking. + */ + +function createOnlineTracker(redisClient) { + // Method to add a user's activity + async function add(user) { + const now = Date.now(); + try { + await redisClient.hSet('online_users', user, now.toString()); + } catch (err) { + console.error('Error setting user activity:', err); + } + } + + // Method to get the list of users active in the last N minutes + async function last(minutes) { + const threshold = Date.now() - minutes * 60 * 1000; + try { + const users = await redisClient.hGetAll('online_users'); + return Object.entries(users || {}).filter(([_, lastActive]) => { + return parseInt(lastActive, 10) >= threshold; + }).map(([id]) => id); + } catch (err) { + console.error('Error retrieving active users:', err); + throw err; + } + } + + return { + add, + last, + }; +} + +// Export the factory function +module.exports = createOnlineTracker;