title: nodejs socket.io cannot connect to server?
body: <p>I've been staring at node.js and socket.io examples all afternoon and i'm trying to piece together a simple page that will tell me how many users I have connected to the server.</p>
  
  <p>I have read the documentation at <a href="http://socket.io/" rel="nofollow noreferrer">http://socket.io/</a> as well as a few tutorials / questions here that outline exactly what i'm trying to do. I have also found <a href="https://stackoverflow.com/questions/5266535/create-simple-node-js-server-and-client">create simple node js server and client</a> which does not help me. </p>
  
  <p>Version Information:  </p>
  
  <blockquote>
    <p>node.js - 0.6.15<br>
    express - 3.0.0alpha1<br>
    socket.io - 0.9.5  (socket.io-client is the same version, however does not find the resource... see comments)<br>
    ejs - 0.7.1 </p>
  </blockquote>
  
  <p>Here is my server code:</p>
  
  <pre><code>var express = require('express'),
      config = {
          port: 4000,
          hostname: 'localhost'
      };
  
  var server = module.exports = express.createServer();
      /* server configuration */
      server.use(express.cookieParser('keyboard unicorn'));
      server.use(express.bodyParser());
      server.use(express.methodOverride());
      server.use(express.session({ secret: 'keyboard unicorn' }));
      server.engine('.html', require('ejs').__express);
      server.set('views', __dirname + '/lib/views');
      server.set('view options', { layout: false });
      server.set('view engine', 'html');
      server.use(server.router);
      server.use('/', express.static(__dirname + '/lib/assets'));
  
  var io = require('socket.io').listen(server);
  
  var connections = { 'length': 0 };
  
  io.sockets.on('connection', function(socket) {
      socket.__fd = socket.fd;
      connections[socket.__fd]=socket.remoteAddress;
      ++connections.length;
      console.log('user connected! There are a total of ' + connections.length + ' users online.');
      return socket.on('disconnect',function(){
          delete conns[socket.__fd];
          --connections.length;
          console.log('user disconnected! There are a total of ' + connections.length + ' users remaining online.');
      });
  });
  
  server.get('/', function( req, res ) {
      res.render('index', {
          'page_title': 'sample application',
          'number_of_connections': connections.length
      });
  });
  
  server.listen(config.port, config.hostname);
  </code></pre>
  
  <p>Here is my client code: </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;&lt;%= page_title %&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div&gt;There is a total of &lt;%= number_of_connections %&gt; user(s) connected.&lt;/div&gt;
  &lt;script src="http://cdn.socket.io/stable/socket.io.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  var socket = new io.Socket('localhost',{'port':4000});
  socket.connect();
  socket.on('connect', function() {
      console.log('connected');
  });
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is what is happening when I run the server, and connect with the client.</p>
  
  <pre><code>$ node server.js
     info  - socket.io started
  </code></pre>
  
  <p>then when I navigate to localhost:4000 on my web browser I get the page with '0' (number_of_connections). Also, I see nothing in the terminal for the server (the on.('connection' is never hit). </p>
  
  <p>On the client, after a second or two, I start to get a massive number of errors (if I leave the console open for a few seconds it crashes the page) see image below:</p>
  
  <p><img src="https://i.stack.imgur.com/mQnLb.png" alt="enter image description here"> </p>
  
  <p>Please, any help with where to start for debugging this would be appreciated! I just want to get this basic example up and running so I can start playing with / understanding nodejs and socket.io!</p>