title: Using socket.io in Express 4 and express-generator's /bin/www
body: <p>So here is the deal: I'm trying to use socket.io in an express project. After Express Js 4 was lauched, i've updated my express-generator and now the app initial functions goes into <code>./bin/www</code> file, including those vars (www file contents: <a href="http://jsfiddle.net/avMa5/" rel="noreferrer">http://jsfiddle.net/avMa5/</a> )</p>
  
  <pre><code>var server = app.listen(app.get('port'), function() {..}
  </code></pre>
  
  <p>(check it by <code>npm install -g express-generator</code> and then <code>express myApp</code></p>
  
  <p>that being said, let's remember how socket.io docs ask us to fire it:</p>
  
  <pre><code>var app = require('express').createServer();
  var io = require('socket.io')(app);
  </code></pre>
  
  <p>Ok but i can't do it inside app.js, like recommended. This should be done in ./bin/www in order to work. in ./bin/www this is what i can do to get it working:</p>
  
  <pre><code>var io = require('socket.io')(server)
  </code></pre>
  
  <p>Ok this works, but i can't use the io var anywhere else, and i really don't want to put my socket.io functions on <code>www</code> file. </p>
  
  <p>I guess this is just basic syntax, but I can't get this to work, not even using <code>module.exports = server</code> or <code>server.exports = server</code> nor <code>module.exports.io = app(io)</code> on www file</p>
  
  <p>So the question is: how can i use socket.io having this /bin/www file as starting point of my app?</p>