How to instantiate Multiple Redis Connections for Publish Subscribe (node.js + node_redis) -


scenario

using node_redis build simple redis pubish subscribe (chat) example: https://github.com/nelsonic/hapi-socketio-redis-chat-example (with hapi.js , socket.io)

we have created node module redis_connection.js in our project ( see: http://git.io/vqaos ) instantiate redis connection because don't want repeating code connects (to rediscloud) multiple times:

var redis = require('redis'); var url   = require('url'); var redisurl    = url.parse(process.env.rediscloud_url); var redisclient = redis.createclient(redisurl.port, redisurl.hostname,                   {no_ready_check: true}); redisclient.auth(redisurl.auth.split(":")[1]);  module.exports = redisclient; 

which use this:

var redisclient = require('./redis_connection.js');  // confirm able connect  rediscloud: redisclient.set('redis', 'working', redisclient.print); redisclient.get('redis', function (err, reply) {   console.log('rediscloud ' +reply.tostring()); }); 

this works fine normal get/set operations redis, when try instantiate multiple connections redis (e.g: 1 publish, subscribe , third get/set keys/values) error:

issue

we seeing following error:

error: connection in subscriber mode, subscriber commands may used

what doing wrong?

full code @ point see issue: http://git.io/vqa6y

note

we tried dig through existing q/a on this, e.g:

but did not find solution exactly matched our situation...

(any suggestions/help appreciated!)

not tested, long comment.

try define redis connection module, 1 regular usage , second 1 solely pubsub subscriptions usage.

add redis_pubsub_connection.js project:

var redis = require('redis'); var url   = require('url'); var redisurl    = url.parse(process.env.rediscloud_url); var redispubsubclient = redis.createclient(redisurl.port, redisurl.hostname,                   {no_ready_check: true}); redispubsubclient.auth(redisurl.auth.split(":")[1]);  module.exports = redispubsubclient; 

and change publish.js require statement to:

var redis = require('./redis_pubsub_connection'); // rediscloud 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -