chriso / redback

A high-level Redis library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pub/Sub example results in exception from node-redis

addisonj opened this issue · comments

When using the example code for pub/sub (slightly modified to have a valid message)

var channel = redback.createChannel('chat').subscribe();

//To received messages
channel.on('message', function (msg) {
   console.log(msg);
});

//To send messages
channel.publish("hello world");

An exception is thrown:

/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:523
            throw err;
                  ^
Error: Connection in pub/sub mode, only pub/sub commands may be used
    at RedisClient.send_command (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:726:15)
    at RedisClient.send_offline_queue (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:378:34)
    at RedisClient.on_ready (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:308:14)
    at RedisClient.on_info_cmd (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:341:14)
    at RedisClient.ready_check.send_anyway (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:365:14)
    at try_callback (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:520:9)
    at RedisClient.return_reply (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:590:13)
    at ReplyParser.RedisClient.init_parser (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/index.js:263:14)
    at ReplyParser.EventEmitter.emit (events.js:96:17)
    at ReplyParser.send_reply (/Users/addisonj/Projects/augment/node_modules/redback/node_modules/redis/lib/parser/javascript.js:297:10)

It seems a client can't be reused for both publish and subscribe and a new connection is needed. As redback is a high level, perhaps when subscribe is called, it creates an new client to listen on?

I shall try and see if I can get it working and open a PR.

Thanks

Yep, this is expected behaviour. The reason it doesn't create a new connection automatically is that there's a lot of edge cases where this would be undesirable or not possible, e.g.

You pass in a Redis connection; the library has no connection details available to create another

var redis = require('redis').createClient()
  , redback = require('redback').use(redis);

You need to create multiple channels and don't want to create a connection for each one (one Redis connection in pub/sub mode will suffice)

var channels = [];
for (var i = 0; i < 100; i++) {
    channel.push(redback.createChannel('chat:' + i));
}

Creating a new connection takes only one line and gives you complete flexibility in deciding which connections are assigned to which structures

var redback = require('redback');

var client1 = redback.createClient()
  , client2 = redback.createClient();

//etc