2

I'm working in realtime posts using socket.io,

Stream is updating realtime but the problem is redis subscription happens multiple times and the post appears multiple times in the stream.

I'm using node.js cluster module to create multiple node clusters. Here is the code,

worker.js

var cluster    = require('cluster');
var server     = new Hapi.Server(config.port,view_options);

var socketIO   = require('socket.io');
var redis      = require("socket.io/node_modules/redis");
var RedisStore = require('socket.io/lib/stores/redis');

var io         = socketIO.listen(server.listener);
var sub        = redis.createClient();
sub.setMaxListeners(0);

io.configure(function() {
    io.set('store', new RedisStore({
        redisSub : sub
    }));
    io.set('transports', ['xhr-polling']);
    io.set('polling duration', '10');
});

if (cluster.isMaster) {
     for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
} 
else {
    sub.subscribe("userstreams");
    io.sockets.on('connection', function(socket) {
    // Realtime streams : subscribe to redis channel userstreams
        sub.on("message", function(channel, member_postid) {
            console.info("-----------realtime post receieved --------",member_postid);                
        });        
    });
}

publisher.js

// head
var redis      = require("socket.io/node_modules/redis");
var pub        = redis.createClient();
// inside a function 
pst = member + "::" + data.pid
pub.publish("userstreams", pst);

The client publishes the postID only once but I'm getting multiple subscriptions in the userstreams channel in server.

I want my server to get the redis subscription only once so that I can send them in socket to a user's stream only once.

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
Srikanth Jeeva
  • 3,005
  • 4
  • 38
  • 58
  • How about having only the master subscribe to redis messages, and then delegating the redis messages to individual worker threads in a round-robbin manner – levi Apr 22 '14 at 13:36
  • can you please give some code samples. – Srikanth Jeeva Apr 25 '14 at 05:55
  • @Srikanth Each worker is subscribed, so each worker will get the update. – Ryann Graham Apr 25 '14 at 16:22
  • @RyanGraham Yea I understand. As levi suggested I have added the redis subscriber to master. Now i'm getting only one subscribtion from redis. But i'm not sure how to send it to workers from master. Added this code in master, io.sockets.emit('userstreamssock', member_postid); but i'm not getting it in the workers. – Srikanth Jeeva Apr 26 '14 at 06:57

1 Answers1

0

You need to configure pub, sub and client for socket.io:

var pub = redis.createClient();
var sub = redis.createClient();
var client = redis.createClient();

and pass them to socket.io

io.set('store', new io.RedisStore({
  redisSub : sub,
  redisPub: pub,
  redisClient: client
}));
Yuri
  • 149
  • 1
  • 5
  • Probably you use new version of Socket.io which does not have `set` method. The recipe above works only for `socket.io@0.9.x`. Another guess - try to use node-redis from `redis` module not from `socket.io`. – Yuri May 29 '14 at 17:03