title: Socket.io TypeError: undefined is not a function
body: <p>I'm making a simple chat app. Connecting, "<a href="http://2.1m.yt/nIfVdnFiI.png" rel="nofollow noreferrer">loggin in</a>" and sending chat messages goes just fine. However when I refresh the page and try to login again I get the error;</p>
  
  <blockquote>
    <p>Missing error handler on <code>socket</code>.
    <br>
    TypeError: undefined is not a function at Socket. '&lt;'anonymous>
    <br>
    [ . . . ] at server.js:30:19</p>
  </blockquote>
  
  <h1>Server.js</h1>
  
  <pre><code>var app = require('express')();
  var http = require('http').Server(app);
  var io = require('socket.io')(http);
  var path = require('path');
  
  var users = [];
  var chat_history = [];
  
  [...]
  
  io.on('connection', function(socket){
      //
      // LOGIN
      ///////////////////////////
      socket.on('login', function(username) {
          if(!socket.username &amp;&amp; username) {
              socket.emit('logged_in', 'successful');
              socket.emit('chat_history', JSON.stringify(chat_history));
              io.emit('notice', username + ' has connected.');
              socket.username = username;
              users.push(username);
  
              update_users();
  
  
              //
              // CHAT MESSAGE RECEIVED
              ///////////////////////////
              socket.on('chat_message', function(msg){
                  var message = getTime() + ' ' + socket.username + ': ' + msg;
  
                  update_chat_history(message);
              });
  
  
              //
              // DISCONNECT
              ///////////////////////////
              socket.on('disconnect', function () {
                  io.emit('notice', socket.username + ' has left the channel.');
                  console.log(socket.username + ' disconnected');
  
                  update_users();
              });
          } else {
              socket.emit('logged_in', 'unsuccessful');
              socket.emit('notice', 'Login was unsuccessful, please refresh the page and try again.');
          }
      });
  
  
      ///////////////////////////////////////////
      //socket.on('error', function (err){
      //    console.error(err.stack);
      //});
      ///////////////////////////////////////////
  });
  
  http.listen(port, ip);
  
  console.log('listening on ' + ip + ':' + port);
  
  
  
  //
  // Functions
  ///////////////////////////
  function getTime() {
      var t = new Date();
      t = '[' + t.getUTCHours() + ' : ' + t.getUTCMinutes() + ']';
  
      return t;
  }
  
  function update_users() {
      //console.log(io.sockets.connected);
      users = [];
  
      for( var client in io.sockets.connected ){
          var username = io.sockets.connected[client].username;
          if(username){
              users.push(username);
          }
      }
  
      users = JSON.stringify(users);
      io.emit('users_online', users);
  }
  
  function update_chat_history(message_to_add){
      chat_history.push(message_to_add);
  
      if(chat_history.length &gt; 20){
          chat_history.splice(0, 1);
      }
  }
  </code></pre>
  
  <p>At socket.on('login') I tested socket not to be undefined after refreshing page or disconnect of con.
  <br>
  I guess it has something to do with it not renewing connection and trying to reconnect to same connection, but I have no idea how or what. Could not find solution at stackoverflow. Help is appreciated.</p>
  
  <blockquote>
    <h3>UPDATE: Blaargh this just doesnt make any at all (╯°□°）╯︵ ┻━┻</h3>
    
    <p>Still the same error. now at server.js:78:11 <br></p>
    
    <p><a href="http://1.1m.yt/kgaZSBfUa.png" rel="nofollow noreferrer">http://1.1m.yt/kgaZSBfUa.png</a> <br></p>
    
    <p>Removed all socket.usernames and changed login to;</p>
  
  <pre><code>socket.on('login', function(username) {
      if( username ) { // TODO what if duplicate username
          socket.emit('logged_in', 'successful');
          socket.emit('chat_history', JSON.stringify(chat_history));
          io.emit('notice', username + ' has connected.');
  
          add_user(socket.id, username);
          [...]
  </code></pre>
    
    <p>Also changed the update_users()</p>
  
  <pre><code>function add_user(id, username){
      users.push({
          'id' : id,
          'username' : username
      });
  
      var json_users = JSON.stringify(users);
      io.emit('users_online', json_users);
  }
  
  function remove_user(id){
      for(user in users){
          if(users[user]['id'] == id){
              users.splice(users[user], 1);
              break;
          }
      }
      var json_users = JSON.stringify(users);
      io.emit('users_online', json_users);
  }
  </code></pre>
  </blockquote>
  
  <p><br></p>
  
  <blockquote>
    <h3>UPDATE: 2</h3>
    
    <p>I still have no clue at all what could have been the problem however somehow I managed to write around it so its working now.</p>
  </blockquote>