I was using Redis version 3 in my Node.js app that published a message. To publish this message, I was able to fetch specific keys that matched a value. Now I have updated to version 4, and it looks like the key fetching part is not working anymore and messages aren't being published. Here is that piece of code:
async myFunction(someData){
console.log(someData) // this is being logged
this.redisClient.keys('REDIS_SUBSCRIBER_SOCKET_CHANNEL_*', (err, ids) => {
if(err){
return console.log(err) // this is not being logged
}
console.log('ids: ' + ids); // this is not being logged
ids.forEach(id => {
console.log(
'Distributing: ' + JSON.stringify(someData),
);
this.publisherClient.publish(id, JSON.stringify(someData));
});
});
}
I can only think that the keys aren't being fetched correctly, but the connection is being made successfully when my app starts.
Node Redis v4 added lots of breaking changes, like support for Promises and elimination of callbacks. It's not a drop in replacement.
You have two choices to make this work. You can enable legacy mode or you can update your code. I would recommend the latter but certainly understand that updating you code is not always a choice.
To enable legacy mode:
const redis = createClient({ legacyMode: true });
To use async/await:
const ids = await redis.keys('REDIS_SUBSCRIBER_SOCKET_CHANNEL_*')
There is a guide to migrating from v3 to v4 on the GitHub repo linked off of the README—which I helped write—listing all the breaking changes.
As an aside, I would also recommend using .scan
over .keys
as it performs better. Calls to .keys
block the single thread that uses to read from and write to the state of Redis. This means that nothing else can happen while it runs. And for it to run is must check every single key in Redis against this pattern. This is bad if you have a lot of keys.