title: Socket.io NodeJS chat : Why do messages emit multiple times?
body: <p>I am trying to create a nodeJS/Socket.io chat app. But whenever I send a message it emits multiple time s for some reason. 
  Client Side:</p>
  
  <pre><code>var socket = io.connect();
  var $users = $('#users');
  var $messageForm = $('#send-message');
  var $messageBox = $('#message');
  var $chat = $('#chat');
  
  socket.emit('new user', "{{user.username}}", function(data){
      if(data) {
          console.log("Connected");
      }
  
  });
  
  socket.on('usernames', function(data){
      var html = '';
      for(var i=0; i &lt; data.length; i++){
          html += data[i] + '&lt;br/&gt;'
      }
      $users.html(html);
  });
  
  $messageForm.submit(function(e){
      e.preventDefault();
      socket.emit('send message', $messageBox.val(), function(data){
          console.log(data);
          $chat.append('&lt;span class="error"&gt;' + "asdfasdfaf" + "&lt;/span&gt;&lt;br/&gt;");
  });
      $messageBox.val('');
  });
  
  socket.on('load old msgs', function(docs){
      for(var i=docs.length-1; i &gt;= 0; i--){
          displayMsg(docs[i]);
      }
  });
  
  socket.on('new message', function(data){
       displayMsg(data);
  });
  
  function displayMsg(data){
      $chat.append(
          '&lt;span class="msg"&gt;&lt;b&gt;' + 
          data.nick + ': &lt;/b&gt;' + data.msg + "&lt;/span&gt;&lt;br/&gt;");
  }
  
  socket.on('whisper', function(data){
      $chat.append(
          '&lt;span class="whisper"&gt;&lt;b&gt;' + 
          data.nick + ': &lt;/b&gt;' + data.msg + "&lt;/span&gt;&lt;br/&gt;");
  });
  </code></pre>
  
  <p>And here is the server side code (this code is inside an app.get() request):</p>
  
  <pre><code>io.sockets.on('connection', function(socket){
      var query = Chat.find({});
      query.sort('-created').limit(8).exec(function(err, docs){
      if(err) throw err;
      // socket.emit('load old msgs', docs);
  });
  
  socket.on('new user', function(data, callback){
      socket.nickname = req.user.username;
      users[socket.nickname] = socket;
  });
  
  
  
  socket.on('send message', function(data, callback){
      var msg = data.trim();
      console.log('after trimming message is: ' + msg);
      var name = req.params.posteruname;
  
      if(name in users){
          users[name].emit('whisper', {msg: msg, nick: socket.nickname});
          console.log('message sent is: ' + msg);
          console.log('Whisper!');
      } else{
          callback('Error!  Enter a valid user.');
      }
  
  
  });
  
  socket.on('disconnect', function(data){
      delete users[req.user.username];
      updateNicknames();
  });
  </code></pre>
  
  <p>So basically the problem is that every time the, 'send message' method is triggered the message is sent out multiple times(the amount of times the page is refreshed) - > I make sure that a new socket is not created when a page is refreshed by the same user, to avoid multiple messages. But multiple messages are still sent. I have been stuck here for a while. Any help is appreciated! </p>