This is more of an architectural question than anything. I am working on a project that uses ExpressJS, Angular, and Socket.io and have run into a challenge of trying to persist the socket.io instantiation so other areas can use it.
Let's jump into what I have:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
In the code above, the io variable can be used to create listeners, send messages, etc. In another file I have my routes that are passed the app var:
require('./app/routes')(app);
In my routes I handle requests, and in one case when a POST comes in I want to fire off a message in socket.io to the connected clients. How do I make the io variable accessible to other areas?
Note: I'm using mongoose with mongodb and noticed that "mongoose.connect(db.url);" is called in the server file, and then in other modules I can require the mongoose.js and call models. How do they maintain the connection across the multiple instantiations?