(function() {

  var socket = io.connect();
  var userList = document.getElementById('users');
  var sendButton = document.getElementById('send');
  var messages = document.getElementById('messages');
  var textIn = document.getElementById('input');
  var user;

  console.log(sessionStorage.getItem("username"));

  if (sessionStorage.getItem("username") == 'null' ||
      sessionStorage.getItem("username") == null){

    user = prompt('username?');

    sessionStorage.setItem('username', user);

  } else {

    user = sessionStorage.getItem("username");

  }

  socket.emit('login', user);

  function addNewUser(name){
    var item = document.createElement('li');
    var txt = document.createTextNode(name);
    item.appendChild(txt);
    userList.appendChild(item);
  }

  function sendMessage(e){
    e.preventDefault();
    var msg = textIn.value;
    socket.emit('new message', {name: user, message: msg});
    textIn.value = '';
  }

  function addMessage(data){
    var entry = document.createElement('div');
    var txt = document.createTextNode(data.name + ': ' + data.message);
    entry.appendChild(txt);
    messages.appendChild(entry);
  }

  function sendOnEnter(e){
    if(e.keyCode === 13){
      sendMessage(e);
    }
  }

  sendButton.addEventListener('click', sendMessage, false);
  textIn.addEventListener('keypress', sendOnEnter, false);

  socket.on('online users', function(data){

    while (userList.firstChild) {
      userList.removeChild(userList.firstChild);
    }

    data.forEach(function(el){
      addNewUser(el);
    });

  });

  socket.on('new user', function(data){
    addNewUser(data);
  });

  socket.on('message', function(data){
    addMessage(data);
  });

})();

@@ -21,16 +21,20 @@
  socket 
+    
 = io.con
@@ -39,23 +39,23 @@
 onnect()
-;%0A  var
+,%0A     
  userLis
@@ -56,16 +56,18 @@
 serList 
+  
 = docume
@@ -92,23 +92,23 @@
 'users')
-;%0A  var
+,%0A     
  sendBut
@@ -144,23 +144,23 @@
 ('send')
-;%0A  var
+,%0A     
  message
@@ -161,16 +161,18 @@
 essages 
+  
 = docume
@@ -204,23 +204,23 @@
 es')
-;%0A  var
+,%0A     
  textIn 
 = do
@@ -215,16 +215,20 @@
  textIn 
+    
 = docume
@@ -257,384 +257,57 @@
 ut')
-;%0A  var user;%0A%0A  console.log(sessionStorage.getItem(%22username%22));%0A%0A  if (sessionStorage.getItem(%22username%22) == 'null' %7C%7C%0A      sessionStorage.getItem(%22username%22) == null)%7B%0A%0A    user = prompt('username?');%0A%0A    sessionStorage.setItem('username', user);%0A%0A  %7D else %7B%0A%0A    user = sessionStorage.getItem(%22username%22);%0A%0A  %7D%0A%0A  socket.emit('login', user);%0A%0A  function addNewUser
+,%0A      user;%0A%0A  var addNewUser = function
 (name)
+ 
 %7B%0A  
@@ -452,36 +452,32 @@
 d(item);%0A  %7D
+;
 %0A%0A  
-function
+var
  sendMessage
@@ -476,19 +476,31 @@
 dMessage
+ = function
 (e)
+ 
 %7B%0A    e.
@@ -631,28 +631,24 @@
  '';%0A  %7D
+;
 %0A%0A  
-function
+var
  addMess
@@ -650,22 +650,34 @@
 dMessage
+ = function
 (data)
+ 
 %7B%0A    va
@@ -857,20 +857,16 @@
 %0A  %7D
+;
 %0A%0A  
-function
+var
  sen
@@ -873,19 +873,31 @@
 dOnEnter
+ = function
 (e)
+ 
 %7B%0A    if
@@ -947,17 +947,329 @@
    %7D%0A  %7D
-%0A
+;%0A%0A  if (sessionStorage.getItem(%22username%22) == 'null' %7C%7C%0A      sessionStorage.getItem(%22username%22) == null) %7B%0A%0A    user = prompt('username?');%0A%0A    sessionStorage.setItem('username', user);%0A%0A  %7D else %7B%0A%0A    user = sessionStorage.getItem(%22username%22);%0A%0A  %7D%0A%0A  socket.emit('login', user);%0A%0A  // event listeners for ui
 %0A  sendB
@@ -1377,24 +1377,69 @@
 r, false);%0A%0A
+  // listen for server sending list of users%0A
   socket.on(
@@ -1622,32 +1622,65 @@
     %7D);%0A%0A  %7D);%0A%0A
+  // listen for new user joining%0A
   socket.on('new
@@ -1732,16 +1732,41 @@
 %0A  %7D);%0A%0A
+  // listen for messages%0A
   socket

add comments and tidy code