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.