title: ExpressJS - Socket.IO with Route Separation
body: <p>I'm trying to get my head around ExpressJS and Socket.IO. I've got my routes in a separate file which I include from my app.js:</p>
  
  <pre><code>var express = require('express')    
    , db = require('./db')
    , mongoose = require('mongoose')
    , models = require('./models/device')
    , http = require('http')
    , path = require('path')
    , app = express()
    , server = http.createServer(app)
    , io = require('socket.io').listen(server)
    , routes = require('./routes/myRoutes');
  </code></pre>
  
  <p>However when I try and emit an event from one of my routes I have no reference to socket.io.</p>
  
  <pre><code>exports.update = function(req, res){
      return Item.findById(req.params.id, function(err, item) {
         // Do some checks and save.
         socket.emit('updated');
      }
  }
  </code></pre>
  
  <p>I understand why this might not be available. Rather I don't understand what the best way to get a handle on socket.io is from another file other than app.js. I was looking at <a href="https://stackoverflow.com/questions/9709912/separating-file-server-and-socket-io-logic-in-node-js">this question</a> (see Ricardo's answer) but I'm still not clear. Ideally I would like to avoid doing this: </p>
  
  <p>routes = requires("routes/myRoutes")(io);</p>