与nodejs的Azure Redis键空间通知问题

问题描述 投票:0回答:1

我正在尝试使用以下代码从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);
  });
});

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

UPDATE2:使用以下代码,我可以收到自己的已发布消息,因此一般而言订阅没有问题:

client.subscribe("pub");

client3.publish("pub", "a test message");
node.js azure redis notifications publish-subscribe
1个回答
1
投票
我终于弄清楚了问题所在,它适用于client.psubscribeclient.on("pmessage",...)
© www.soinside.com 2019 - 2024. All rights reserved.