title: Giving different number variables to different browsers in Node.js
body: <p>I am trying to give different numbers to each of my clients. When I click the button on first browser it must say <code>usernumber is 1</code> and when I do this in second browser I expect it to say <code>usernumber is 2</code>. It actually says <code>usernumber is 2</code> but only when I press the button twice or more.</p>
  
  <p>After I press the button on second browser, I open the first browser and press the button, now it says <code>usernumber is 2</code> and when I press two or more times it says <code>usernumber is 1</code>.</p>
  
  <p>So it is not stable at all and I always have to press this button to keep it in correct value.</p>
  
  <p>If you run my files you will see what I mean better. I use google chrome, my express version is 3.0.0 and my socket.io version is 0.9.16.</p>
  
  <hr>
  
  <p>Here is my app.js and index.html files:</p>
  
  <h2>Index.html</h2>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt; client number &lt;/title&gt;
  
  &lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
  &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  var ax;
  jQuery(function($) {
    var socket = io.connect();
  
    socket.emit('getbrowser1', {});
    socket.on('getbrowser1f', function(data) {
      ax = data;
    });
  
    var butondene1 = document.getElementById("butondene1");
    butondene1.addEventListener('click', function() {
      socket.emit('getbrowser1', {});
      document.getElementById("playerwho").innerHTML = "usernumber is " + ax;
    });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;button id="butondene1"&gt;butondeneme1&lt;/button&gt;
  &lt;text  id="playerwho"&gt;player who ? &lt;/text&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h2>App.js</h2>
  
  <pre><code>var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);
  
  var usernumm = 0;
  var usernum1 = [];
  
  app.use(express.static(__dirname + '/'));
  
  server.listen((process.env.PORT || 5000));
  
  io.sockets.on('connection', function(socket) {
    var endpoint = socket.manager.handshaken[socket.id].address;
  
    console.log('***New connection from ' + endpoint.address + ':' + endpoint.port);
  
    usernumm++;
    usernum1[usernumm] = endpoint.port;
  
    console.log('usernum' + usernumm + 'geldi' + findusernum());
  
    socket.on('disconnect', function() {
      usernumm--;
  
      console.log('***Client disconnected');
    });
  
  
    socket.on('getbrowser1', function(data) {
      var a = findusernum();
      io.sockets.emit('getbrowser1f', a);
    });
  
    //finds number of online users
  
    function findusernum() {
      for (var i = 0; i &lt; 4; i++) {
        if (usernum1[i] == endpoint.port) {
          return i;
        } else {}
      }
    }
  
  });
  </code></pre>