0

I use socket.io to open a socket from my client to my node.js server. I also push the number of connected users to all clients every 10 seconds using io.sockets.clients().length, the problem with this value is, that it doesn't check if the same user has opened multiple tabs with the same page.

Is there a way to get a list of unique clients that are connected in socket.io?

Christian Strang
  • 8,470
  • 5
  • 42
  • 53
  • What do you mean "does not check" -- it reports more open connections than you expect or less than you expect? – exebook Nov 24 '13 at 03:41
  • the value is correct, it gives me all open sockets, though I need a different value, instead of all open sockets I need "all unique clients that are connected". – Christian Strang Nov 24 '13 at 11:53
  • do you mean that two open tabs share the same socket in your case? (chrome?) – exebook Nov 24 '13 at 11:55
  • Two open tabs by the same user are two sockets, which is fine. But I need to figure out how many clients are uniquely connected to my server. I'm not sure how to do this with socket.io, do I just generate a value on the client side which I send to the server on the connection? (the value would be a combination of the "user IP"+"browser"+"browser version"+etc.) – Christian Strang Nov 24 '13 at 12:05
  • what is your definition of a client? a person? a computer? a browser? – exebook Nov 24 '13 at 20:36

2 Answers2

1

I suggest you use a browser cookies.

Here is the example:

How do I create and read a value from cookie?

Community
  • 1
  • 1
exebook
  • 32,014
  • 33
  • 141
  • 226
0

I thing you are locking unique ID for each person.

Client code.

//When user connect to socket server to emit some info event like this socket.emit('userInfo','clintUniqueID');//like mailid ID,username,anything but its unique

//When user open other tabs

socket.on('multipleTabs',function(data){
    //Your operation for client side    
     //example 
      alert('you are open multiple tabs');});

Server code:

var connection={}; //socket.io connection each user 
var usersList=[];//This list only contain unique ID for each connection
socket.on('userInfo',function(data){//emit by client (user)
 connection[socket.id]=socket; // just for connection
 var(var i=0;i<usersList.length;i++)
     {
           if(usersList[i]==data)//Here you store user unique ID like mailID,Username etc
             {
                  //user open multiple tabs  
                  //Any operation server site
                  //if you want to do any operation in  client site
                    socket.emit('multipleTabs','userAlreadyExit');



       }else
           {
                        if(i==usersList.length-1)
                           {
                               usersList.push(data);//store socket.id 
                                  }

                     }
 });

Now it will work check it