Skip to content
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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vinybk
Copy link

@vinybk vinybk commented Jan 19, 2025

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:

  • Removed dependency on online by creating a new file, online-tracker.js, which replicates the original functionality.
  • Switched from callbacks to promises.
  • Updated Redis commands and added try/catch for error handling.
  • Refactored Redis initialization into a dedicated function 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:

  • Redis 7.0.15
  • Node.js 22.12.0
  • Express 5.0.1
  • Debian 12

Let me know if any changes are needed!

Copy link
Member

@wesleytodd wesleytodd left a 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
Copy link
Member

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');
Copy link
Member

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');
Copy link
Member

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());
Copy link
Member

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);
}
Copy link
Member

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.

@vinybk vinybk marked this pull request as draft January 20, 2025 20:49
@vinybk
Copy link
Author

vinybk commented Jan 20, 2025

Okay, appreciate the inputs. I'll work on that changes and re-request review once it's done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants