0

I am using node js 4.2.6 version and using 1.4.5 socket.io version.I am using socket.io-redis module as the adaptor for socket.io. I am having two server with port 3000 and 4000. I added timer to print total number of connection sockets as below.

var socketio = require('socket.io')
var redis = require('socket.io-redis'),
io=socketio.listen(server); 
io.adapter(redis({ host: '127.0.0.1', port: 6379 }));
setInterval(function() {
                console.log("The total sockets are:::::::::::::", Object.keys(io.sockets.connected).length);
            }, 5000 );

In one server I am getting correct client count , but in another I am getting 0.How can synchronize the socket.io clients between the server.

Soorya Prakash
  • 921
  • 3
  • 9
  • 29
  • Why do you have two servers? AFAIK, `socket.io-redis` will not sync sockets/connections, it will allow you to reach clients connected to different servers (which are generally _physical_ servers). – robertklep Aug 02 '16 at 07:41
  • Can you please provide me somce sample to code to get clients connected to different servers? – Soorya Prakash Aug 02 '16 at 08:09
  • I think you'll need to implement some sort of coordination between your servers if you want to track the total number of clients connected. – robertklep Aug 02 '16 at 08:13

2 Answers2

0

You have to create a Socket Model and store the data ( in Mongo or Redis... whatever you're using ) so both server instances can request and access that information properly.

Squivo
  • 917
  • 1
  • 10
  • 21
0

When use https://github.com/socketio/socket.io-redis there are many things to do:

  1. io.adapter.remoteJoin(socket.id, 'room_name_1', function(){}) // add a client to a room
  2. io.adapter.allRooms(function (err, rooms) {}) // get all room
  3. io.adapter.clients(rooms, function (err, clients) {})// clients.length count total client in all room.

Your code io.sockets.connected just count on one server (computer/machine)

tuananh
  • 747
  • 8
  • 17