【问题标题】:nodeJS - ioredis NPM Module - Having problem's with subscriber eventsnode JS - ioredis NPM 模块 - 订阅者事件有问题
【发布时间】:2019-08-03 18:30:18
【问题描述】:
const Redis = require('ioredis');

const sub = new Redis();
const pub = new Redis();

sub.on('subscribe', (channel, count) => {
    console.log("Subbed to channel: " + channel);
})

sub.on('message', (channel, message) => {
    console.log("Message recieved: " + message);
})

setTimeout(() => {
    console.log("Attempting");
    sub.subscribe("hey");
    pub.publish("hey", "msg");
}, 2000);

这是我一直用来测试的代码,它会产生以下控制台输出:

Attempting

Message recieved: msg

预期输出:

Attempting

Subbed to channel: hey

Message recieved: msg

sub.on('subscribe') 事件似乎没有触发。

【问题讨论】:

  • 您在哪里找到了sibscribe 事件?我在文档中找不到它。

标签: javascript node.js npm redis ioredis


【解决方案1】:

试试这样:

sub.subscribe(channel, (error, count) => {
    if (error) {
        throw new Error(error);
    }
    console.log(`Subscribed to ${count} channel. Listening for updates on the ${channel} channel.`);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    相关资源
    最近更新 更多