Redis/Node.js - 2 个客户端(1 个发布/订阅)导致写入问题 [英] Redis / Node.js - 2 clients (1 pub/sub) causing issues with writes

查看:37
本文介绍了Redis/Node.js - 2 个客户端(1 个发布/订阅)导致写入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建两个客户端;一个是发布/订阅,另一个是标准连接.这不可能吗?必须有一种方法可以将其抽象为工作:) 基本上,如果我在运行 test.js 后执行 get key,我看到的只是valueBefore".输出:

Trying to create two clients; one is pub/sub, the other is a standard connection. Is this not possible? There must be a way to abstract this to work :) Basically, if I do a get key after running test.js, all I see is 'valueBefore'. The output:

node test.js 
Reply: OK

/Users/franklovecchio/Desktop/development/node/node_modules/redis/index.js:487
        throw new Error("Connection in pub/sub mode, only pub/sub commands may
              ^
Error: Connection in pub/sub mode, only pub/sub commands may be used
    at RedisClient.send_command (/Users/franklovecchio/Desktop/development/node/node_modules/redis/index.js:487:15)
    at RedisClient.<anonymous> (/Users/franklovecchio/Desktop/development/node/node_modules/redis/index.js:597:27)
    at Object._onTimeout (/Users/franklovecchio/Desktop/development/node/distributed-cache/client/test.js:19:12)
    at Timer.callback (timers.js:83:39)

代码:

var redis = require('redis');

var client1 = redis.createClient();
var client2 = redis.createClient();

client2.on('message', function (channel, message) {
    console.log('Received a message on channel: ' + channel);       

    client1.set('key', message, redis.print);

});

client2.subscribe('channel');

client1.set('key', 'valueBefore', redis.print);

setTimeout(
    function() {
        client2.publish('channel', 'valueAfter');
    },3000
);

推荐答案

您可能需要从 client1 发布消息,因为 client2 专用于侦听特定频道上的消息.node_redis自述文件中几乎没有关于这种行为的描述:

You probably need to publish messages from client1 since client2 is dedicated to listening for messages on certain channel. Few words about this behavior are written in node_redis readme:

如果您需要在 pub/sub 模式下向 Redis 发送常规命令,只需打开另一个连接.

If you need to send regular commands to Redis while in pub/sub mode, just open another connection.

这篇关于Redis/Node.js - 2 个客户端(1 个发布/订阅)导致写入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆