title: Why is Socket.IO adding two clients when only one is connecting?
body: <p>In my app, the user goes to the home screen, chooses their username, then navigates to another page, from which they can go to the page where the room is entered.</p>
  
  <p>I have some code that increments a variable <code>clients</code> every time user joins a room. But, whenever a new user joins, <code>clients</code> is incremented by the amount of connected users. This is not the desired behaviour. My code is below:</p>
  
  <pre><code>app.post('/race', function(req,res) {
    let race = io.of('/race').on('connection', (socket) =&gt; {
      console.log('A user has entered the race!');
      socket.on('joinRoom', function(roomName) {
        socket.room = roomName;
        console.log(socket.room);
  
        clients++;
        console.log(clients);
      });
  
      socket.on('disconnect', function() {
        clients--;
        console.log("A user has disconnected");
        console.log(clients);
      });
    });
  
    res.sendFile(path.join(__dirname, '/client/race/index.html'));
  });
  </code></pre>
  
  <p>I have removed some functions from my code for brevity's sake. But the main functionality of adding users to <code>clients</code> is still there. When only one user is connecting and disconnecting, it works fine, but when another user joins, it adds 2, bringing the total up to 3. When a third joins, it adds 3, bringing the total to 6. </p>
  
  <p>There must be some problem with the <code>socket.on('joinRoom</code> function - it appears to be being called the amount of times there are connected users. Note that <code>console.log('A user has entered the race!');</code> is not called twice.</p>
  
  <p>Why is this problem occuring?</p>