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:
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');
However when I try and emit an event from one of my routes I have no reference to socket.io.
exports.update = function(req, res){
return Item.findById(req.params.id, function(err, item) {
// Do some checks and save.
socket.emit('updated');
}
}
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 this question (see Ricardo's answer) but I'm still not clear. Ideally I would like to avoid doing this:
routes = requires("routes/myRoutes")(io);