-
-
Notifications
You must be signed in to change notification settings - Fork 17.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: update online example to support Redis v4+ and remove outdated dependency #6286
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I am happy to see these get updated. Unfortunately there are some things in this PR which likely need to be fixed before we can merge.
online.add(req.headers['user-agent']); | ||
app.use(async (req, res, next) => { | ||
try { | ||
// fire-and-forget |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not fire and forget with an await
and try/catch
. The original design of this was because they didn't want to fail the request if the write failed and still fall back to the response handling.
That said, as an example goes I would prefer we just handle this type of error more robustly to show how. So I would suggest you change the catch
to not pass the error to next, just console.error(err)
it.
// $ redis-server | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var express = require('../..'); | ||
var online = require('online'); | ||
var online = require('./online-tracker'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just remove this entire file and pull the logic into here. It is an example, and examples this small should be easy to follow in a single file.
@@ -0,0 +1,44 @@ | |||
'use strict'; | |||
|
|||
const { createClient } = require('redis'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not using this in this file. Take my other suggestion and move this logic into the index.js
and you can just remove this line. Additionally, I dont think any of this code works because you are not calling your exported createOnlineTracker
anywhere.
async function add(user) { | ||
const now = Date.now(); | ||
try { | ||
await redisClient.hSet('online_users', user, now.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure where this came from (was lifted from the online
package maybe?), but either way I think this is not the optimal way to achieve this. New versions of redis support expiry on hash keys via hexpire
which is likely the optimal way to do this. Either way, this is an forever growing hash which is never good.
If you think jumping to a new feature is a bad idea (which is a valid opinion I think), then you could add some code to remove the items which are expried in the last
method.
await redisClient.hSet('online_users', user, now.toString()); | ||
} catch (err) { | ||
console.error('Error setting user activity:', err); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticing now that you have both this and the call to add
in a try/catch, which is unnecessary.
Okay, appreciate the inputs. I'll work on that changes and re-request review once it's done. |
This PR updates the Search example to address issues caused by breaking changes in Redis v4+.
The online example was broken due to Redis updates and its reliance on the outdated online package by TJ Holowaychuk, which is no longer maintained and has only 24 weekly downloads.
I considered removing the example or replacing
online
with another library but opted to add a new file that substitutes the dependency.Key changes:
online
by creating a new file,online-tracker.js
, which replicates the original functionality.try/catch
for error handling.initializeRedis
to make sure that it completes before the server starts.Note: the original
online
module was optimized for performance. The substitute file prioritizes simplicity to make it easier to understand, considering it is now part of an example rather than a standalone module.Tested locally with:
Let me know if any changes are needed!