title: node.js + socket.io + express.js: No connection
body: <p>I've been trying to set up a simple socket.io example, but it refuses to work properly. The client runs on Apache2 and should access the node.js-server via the port 3000.</p>
  
  <p>There doesn't seem to be <em>any</em> connection and it doesn't seem to matter whether the server's actually running - if the node.js-process is manually stopped on the server (<code>$ kill xyz</code>), my client-console throws no errors whatsoever. The exact same result occurs when the app is running (<code>info  - socket.io started</code>).</p>
  
  <p><strong>Server</strong>:</p>
  
  <pre><code>// Check dependencies &amp; setup backend
  var express = require('express');
  var server = express();
  var http = require('http');
  var app = http.createServer(server); // somehow needed? ("Socket.IO's `listen()` method expects an `http.Server` instance as its first parameter.")
  var socket = require('socket.io');
  // listen @ port 3000 as definied by proxy
  server.listen(3000);
  // listen w/ socket.io as well
  var io = socket.listen(app);
  
  ...
  
  // socket.io
  // TODO: test, added 01/03/14 
  io.set('match origin protocol', true);
  io.set('log level', 2);
  var clients = io.sockets.clients();
  io.sockets.on('connection', function(client) {
      console.log('Connection from ' + client);
  
      client.emit('news', {
          hello : 'world'
      });
  
      client.on('event', function(data) {
          console.log(data);
      });
  });
  </code></pre>
  
  <p><strong>Client</strong>:</p>
  
  <pre><code>&lt;script type="text/javascript" src="./js/socket.io.js" &gt;&lt;/script&gt;
          &lt;script&gt;
              try {
                  var socket = io.connect('https://localhost:3000');
                  console.log(io);
                  console.log('check 1', socket.socket.connected); // false
  
                  socket.on('connect', function() {
                      console.log('check 2', socket.socket.connected); // not reached
                  });
  
                  socket.on('news', function(data) {
                      console.log(data);
                  });
  
              } catch(error) {
                  console.log(error);
              } finally {
                  console.log('fin');
              }
          &lt;/script&gt;
  </code></pre>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>This code works, but <em>only locally</em>. I've even installed Apache 2.4 on the server to ensure I've access to WebSockets (I read somewhere they weren't available in 2.2.x). 
  <strong>Server:</strong></p>
  
  <pre><code>var express = require('express');
  var app = express();
  var server = app.listen(3000);
  var io = require('socket.io').listen(server);
  
  app.use(express.static(__dirname + '/'));
  
  io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
      console.log(data);
    });
  });
  </code></pre>
  
  <p><strong>Client:</strong></p>
  
  <pre><code>&lt;script type="text/javascript" src="js/socket.io.js" &gt;&lt;/script&gt; 
          &lt;script&gt;
              var socket = io.connect('ws://localhost:3000');
              socket.on('news', function(data) {
                  console.log(data);
                  socket.emit('my other event', {
                      my : 'data'
                  });
              });
  
          &lt;/script&gt;
  </code></pre>