0

this is the code I have in an index.html (client). Im wondering why the number is not being incremented. I expect on each time a socket connect I give it a unique name.

 var number = 0;

    socket.on("connect",function(){
         number = number+1;

        socket.emit("add user","user"+ number )


    })

but when i have this server

io.on("connection", function(socket){

    console.log("io.onconnection")
    socket.on("add user", function(data){ 
    console.log(data)
    })
});

on each connection i get logged user1, why isn't the second connection user2? thanks

jack blank
  • 5,073
  • 7
  • 41
  • 73

1 Answers1

1

Because javascript is a client side, and number will always be 0 every time the page refresh/load.

You can count the number client connected in your websocket, server then pass it to js and then you can increment.

VMcreator
  • 833
  • 8
  • 17
  • thanks for your answer. It was helpful. but i was trying to figure out which client performed action and i thought i could do that from the client side.. can you also answer this question that pertains to my problem please . http://stackoverflow.com/questions/31911606/how-can-you-tell-which-socket-connection-clicked-a-button-using-socket-io – jack blank Aug 10 '15 at 05:53