title: get TypeError: Cannot read property 'on' of undefined
body: <p>I am getting following error while implementing <a href="http://danielnill.com/nodejs-tutorial-with-socketio/" rel="nofollow">danielnill</a> example</p>
  
  <p>when i added connection action and client side code in this example i got this error.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>/home/oneadmin/Desktop/test-projects/nodejs/basic-example/server.js:40
  io.sockets.on('connection', function(socket){
            ^
      TypeError: Cannot read property 'on' of undefined
          at Object.&lt;anonymous&gt; (/home/oneadmin/Desktop/test-projects/nodejs/basic-example/server.js:40:11)
          at Module._compile (module.js:449:26)
          at Object.Module._extensions..js (module.js:467:10)
          at Module.load (module.js:349:32)
          at Function.Module._load (module.js:305:12)
          at Function.Module.runMain (module.js:490:10)
          at startup (node.js:124:16)
          at node.js:807:3
  </code></pre>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>var http = require("http");
  var url = require('url');
  var fs = require('fs');
  var io = require('socket.io');
  
  var server = http.createServer(function(request, response){
      console.log('Connection');
      var path = url.parse(request.url).pathname;
  
      switch(path){
          case '/':
              response.writeHead(200, {'Content-Type': 'text/html'});
              response.write('hello world');
              response.end();
              break;
          case '/socket.html':
              fs.readFile(__dirname + path, function(error, data){
                  if (error){
                      response.writeHead(404);
                      response.write("opps this doesn't exist - 404");
                  }
                  else{
                      response.writeHead(200, {"Content-Type": "text/html"});
                      response.write(data, "utf8");
                  }
                  response.end();
              });
              break;
          default:
              response.writeHead(404);
              response.write("opps this doesn't exist - 404");
              response.end();
              break;
      }
  
  });
  
  server.listen(8001);
  io.listen(server);
  io.sockets.on('connection', function(socket){
      socket.emit('message', {'message': 'hello world'});
  });
  </code></pre>
  
  <p><strong>socket.html</strong></p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script src="/socket.io/socket.io.js"&gt; &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;script&gt;
              var socket = io.connect();
  
              socket.on('message', function(data){
                  console.log(data.message);
              });
  
          &lt;/script&gt;
  
      &lt;div&gt; This is our socket.html file&lt;/div&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Pls help me to understand error and how do i solve it?</p>