title: Node chat gives throw er; //unhandled error event
body: <p>So i set up a small basic chat system:</p>
  
  <p>Server code:</p>
  
  <pre><code>    var io = require('socket.io').listen(8000);
  
  // open the socket connection
  io.sockets.on('connection', function (socket) {
  
     // listen for the chat even. and will recieve
     // data from the sender.
     socket.on('chat', function (data) {
  
        // default value of the name of the sender.
        var sender = 'unregistered';
  
        // get the name of the sender
        var name = socket.nickname;
           console.log('Chat message by ', name);
           console.log('error ', err);
           sender = name;
  
  
        // broadcast data recieved from the sender
        // to others who are connected, but not 
        // from the original sender.
        socket.broadcast.emit('chat', {
           msg : data, 
           msgr : sender
        });
     });
  
     // listen for user registrations
     // then set the socket nickname to 
     socket.on('register', function (name) {
  
        // make a nickname paramater for this socket
        // and then set its value to the name recieved
        // from the register even above. and then run
        // the function that follows inside it.
        socket.nickname = name;
  
           // this kind of emit will send to all! :D
           io.sockets.emit('chat', {
              msg : "naay nag apil2! si " + name + '!', 
              msgr : "mr. server"
        });
     });
  
  });
  </code></pre>
  
  <p>Client side:</p>
  
  <pre><code>&lt;html&gt;
     &lt;head&gt;
        &lt;script src="http://localhost:8000/socket.io/socket.io.js"&gt;&lt;/script&gt;
        &lt;script src="http://code.jquery.com/jquery-1.6.2.min.js"&gt;&lt;/script&gt;
        &lt;script&gt;
           var name = '';
           var socket = io.connect('http://localhost:8000');
  
           // at document read (runs only ones).
           $(document).ready(function(){
              // on click of the button (jquery thing)
              // the things inside this clause happen only when 
              // the button is clicked.
              $("button").click(function(){
  
                 // just some simple logging
                 $("p#log").html('sent message: ' + $("input#msg").val());
  
                 // send message on inputbox to server
                 socket.emit('chat', $("input#msg").val() );
  
                 // the server will recieve the message, 
                 // then maybe do some processing, then it will 
                 // broadcast it again. however, it will not 
                 // send it to the original sender. the sender
                 // will be the browser that sends the msg. 
                 // other browsers listening to the server will
                 // recieve the emitted message. therefore we will
                 // need to manually print this msg for the sender.
                 $("p#data_recieved").append("&lt;br /&gt;\r\n" + name + ': ' + $("input#msg").val());
  
                 // then we empty the text on the input box.
                 $("input#msg").val('');
              });
  
              // ask for the name of the user, ask again if no name.
              while (name == '') {
                 name = prompt("What's your name?","");
              }
  
              // send the name to the server, and the server's 
              // register wait will recieve this.
              socket.emit('register', name );
           });
  
           // listen for chat event and recieve data
           socket.on('chat', function (data) {
  
              // print data (jquery thing)
              $("p#data_recieved").append("&lt;br /&gt;\r\n" + data.msgr + ': ' + data.msg);
  
              // we log this event for fun :D
              $("p#log").html('got message: ' + data.msg);
  
           });
        &lt;/script&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;input type="text" id="msg"&gt;&lt;/input&gt;&lt;button&gt;Click me&lt;/button&gt;
        &lt;p id="log"&gt;&lt;/p&gt;
        &lt;p id="data_recieved"&gt;&lt;/p&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>While it works pretty well, everytime i pass something to the server from the client i get an: 
  "throw er; // unhandled 'error' event.</p>
  
  <p>ReferenceError: err is not defined.</p>