Redis将消息发布到nchan队列

时间:2020-01-16 10:30:10

标签: nginx redis nginx-config nchan

假设我们为nchan pub / sub提供了简单的nginx配置:

upstream redis_upstream {
    nchan_redis_server "redis://redis";
}


server {
    listen 80;
    listen [::]:80;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name localhost;

    nchan_storage_engine "redis";
    nchan_message_buffer_length 100;
    nchan_message_timeout 5m;

    location = /sub {
        nchan_subscriber;
        nchan_channel_id $arg_campaignId;
        nchan_redis_pass redis_upstream;
        nchan_use_redis on;
    }

    location = /pub {
        nchan_publisher;
        nchan_channel_id $arg_campaignId;
        nchan_redis_pass redis_upstream;
        nchan_use_redis on;
    }

    location / {
        try_files $uri $uri/ =404;
    }

}

此配置运行良好,但不适合我的项目。我在nodejs应用程序中有消息发布逻辑,因此每次需要将消息推送给订阅者时都非常不方便地调用nginx。但是,有一种方法可以使用PUBLISH redis命令发布消息,但是nchan使用messagepack,我一直在猜测正确的消息格式。 现在我有了这段代码(“ msgpack5”:“ ^ 4.2.1”):

publish(campaignId: number, quest: string) {
    const channelId = `{channel:/${campaignId}}:pubsub`;
    const now = String(new Date().getTime()).slice(0, 10);
    const msg = [
      'ch+msg',
      `${campaignId}`,
      300, // ttl
      Number(now),
      0, // tag
      0, // prev_time
      0, // prev_tag
      quest,
      'application/json',
    ];
    const encoded = this.msgpack.encode(msg);
    return this.redis.publishBuffer(channelId, encoded.slice());
}

执行上面的代码后,Nginx记录

2020/01/16 09:09:04 [error] 7#7: REDISTORE: msgpack parsing error: Error reading type marker
2020/01/16 09:09:04 [error] 7#7: REDISTORE: thought there'd be a channel id around for msg

相关的github issue

nginx-extras软件包(1.14.0-0ubuntu1.7)ubuntu 18.04中的Nchan版本

0 个答案:

没有答案