开发者

Redis / Node.js - 2 clients (1 pub/sub) causing issues with writes

开发者 https://www.devze.com 2023-04-03 08:44 出处:网络
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 towork :)Basically, if I do a get key after running test.js,

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
        thr开发者_Python百科ow 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)

The code:

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


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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号