node.js - Which from these approaches is better while using Socket IO, Node and Redis for production? -
i want know best approach production.
1 create redis client each socket connected socket io in node backend. example:
io.on('connection', function (socket) { var redis = redis.createclient(); redis.on("message", function(channel, data) { //emit sockets, emit socket or broadcast } }
2. create 1 redis client , store in hashmap (nodejs/js object) connected sockets , when receive redis event emit sockets want (you may use whatever filtering logic want on users array). example:
var redis = redis.createclient(); redis.subscribe('some-channel'); redis.on("message", function(channel, data) { (var = 0; < users.length; i++) { io.to(users.socketid).emit("your-event", "your-data"); } } io.on('connection', function (socket) { users.push({ socketid: socket.id }); }
in first approach worried about
- the performance, because iterate on connected clients in loop while second approach has 1 redis client each socket , emits that
- when close 1 redis connection ? in second approach can close redis client of respective socket on socket disconnect event
your other possible approaches more welcomed, these 2 approaches know far.
Comments
Post a Comment