title: Why does nodejs server with socket.io causing endless loop on connection?
body: <p>We are a little group of 2 developers trying to implement a Simple Chat Server with node js and socket.io. We are using github. When I pulled my partners changes and does npm start, I found my console outputing "a user is connected" in an <strong>endless loop</strong>.</p>
  
  <p>Here is the code (the part that is meant to output a user connected once: <code>socket.on('user login', function(newuser)</code>...) .
  <strong>index.js</strong>:</p>
  
  <pre><code>//dependencies
  var app = require('express')();
  var http = require('http').Server(app); //http server
  //initialize a new instance of socket.io by passing http server
  var io = require('socket.io')(http);
  var users = []; //List of online users
  app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
  });
  
  io.on('connection', function(socket){
    console.log('a user connected');
  
    socket.on('disconnect', function(){
      console.log('user: '+socket.username + ' logged out.');
      var date = new Date();
      io.emit('info message', {timestamp: date.toUTCString(), user: socket.username + ' logged out.'});
      //TO DO: Delete user from list.
    });
  
  ...
  </code></pre>
  
  <p><strong>index.html</strong> script:</p>
  
  <pre><code>&lt;script src="https://cdn.socket.io/socket.io-2.1.1.js"&gt;&lt;/script&gt;
  
  
  &lt;script&gt;
  
  $(function () {
            var socket = io();
  
            /*
            * On submit of form with id 'entername' client sends entered username to server.
            * After sending div-Container loginDiv will be hidden and messageDiv will be shown.
            * And last: set value of usernameinput to ''.
            * */
            $('#enterName').submit(function () {
               //console.log($('#userNameInput').val());
               socket.emit('user login',$('#userNameInput').val());
               $('#loginDiv').hide();
               $('#messageDiv').show();
               $('#userNameInput').val('');
               return false;
            });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>package.json</strong> dependencies:</p>
  
  <pre><code>"dependencies": {
      "body-parser": "^1.18.3",
      "express": "^4.16.3",
      "socket.io": "^2.1.1"
    }
  </code></pre>