nodejs 的 Azure Redis 密钥空间通知问题

Azure Redis keyspace notification issue with nodejs

我正在尝试使用以下代码从 Azure Redis 密钥空间通知获取所有通知,但它没有收到任何通知,我检查了密钥,它是在 Azure Redis 实例中创建的。此外,我通过将 notify-keyspace-events 设置为 Kxg 来启用 Azure Redis 密钥空间通知。

感谢任何帮助:

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);
  });
});

更新: 我使用这里的代码片段尝试了 C# 代码 https://github.com/rustd/RedisSamples/blob/master/HelloWorld/KeySpaceNotifications.cs,它工作正常,这意味着我的 NodeJs 代码接收事件有问题。

更新2: 使用下面的代码我可以收到自己发布的消息,所以一般来说订阅是没有问题的:

client.subscribe("pub");

client3.publish("pub", "a test message");

我终于弄明白了,它适用于 client.psubscribeclient.on("pmessage",...)