6

Currently I am using node.js and redis to build a app, the reason I use redis is because of the publish/subscribe feature. The app is simply to notify the manager when user comes into the user or out of room.

function publishMsg( channel , mssage){
    redisClient.publish(channel,JSON.stringify());
}

publishMsg('room/1/user/b',{'room':1,'user':'b'});
publishMsg('room/1/user/c',{'room':1,'user':'c'});
publishMsg('room/2/user/b',{'room':2,'user':'b'});
publishMsg('room/2/user/c',{'room':2,'user':'c'});

function subscribe(pattern){
    redisClient.psubscribe(pattern);
    redisClient.on('pmessage', function(pattern, channel, message){     
        console.log('on publish / subscribe   ',  pattern+"   "+channel+"    "+message+"   "+ JSON.parse( message) );
    });
}

since I want to listen to the join and disjoin event, my question is should I use two redisclient to listen these two events, like

   redisClient1.psubscribe('room/*/user/*/join');
   redisClient2.psubscribe('room/*/user/*/disjoin');

or just use one redisclient to listen and seperate the logic inside the callback

   redisClient2.psubscribe('room/*/user/*');

I know these two ways are possible, But I don't know how in reality people use them, in which condition?

4

1 回答 1

11

您可以安全地重复使用同一个 Redis 连接来订阅多个频道,但不能使用同一个连接来执行其他(非订阅相关的)任务。Redis 文档指出:

一旦客户端进入订阅状态,它就不应该发出任何其他命令,除了额外的 SUBSCRIBE、PSUBSCRIBE、UNSUBSCRIBE 和 PUNSUBSCRIBE 命令。

我总是在 Node 中使用单个 Redis 连接来收听订阅的频道。

于 2013-10-18T16:37:13.200 回答