I am trying to get all notifications from Azure Redis keyspace notification using the following code, but it doesn't receive any notification, I checked the key, it got created in the Azure Redis instance. Also, I enabled Azure Redis keyspace notification by setting the notify-keyspace-events
to Kxg
.
Any help is appreciated:
const redis = require('redis');
process.env.REDIS_CACHE_HOST_NAME = "<cachename>.redis.cache.windows.net";
process.env.REDIS_CACHE_KEY = "<cachekey>";
const client = redis.createClient(
6380,
process.env.REDIS_CACHE_HOST_NAME,
{
auth_pass: process.env.REDIS_CACHE_KEY,
tls: { servername: process.env.REDIS_CACHE_HOST_NAME },
});
const client2 = redis.createClient(
6380,
process.env.REDIS_CACHE_HOST_NAME,
{
auth_pass: process.env.REDIS_CACHE_KEY,
tls: { servername: process.env.REDIS_CACHE_HOST_NAME },
});
const db = 0;
const notificationChannel = "__keyspace@" + db + "__:*";
client.on("message", function (channel, message) {
console.log('100', channel , message);
});
client.subscribe(notificationChannel, function (err) {
console.log('101' , err);
client2.set("foo", "bar", 'EX', 10, function (err) {
console.log('102' , err);
});
});
Update: I tried C# code using the snippet here https://github.com/rustd/RedisSamples/blob/master/HelloWorld/KeySpaceNotifications.cs and it works fine which means something wrong with my NodeJs code to receive the events.
UPDATE2: Using the following code I can receive my own published message, so there is no problem with subscription in general:
client.subscribe("pub");
client3.publish("pub", "a test message");
I've finally figured it out, it works with client.psubscribe
and client.on("pmessage",...)
.