I'm trying to separate my socket.io code into a separate file (socket.js) from my main file (app.js). However, I need to define my io object in app.js, which is also used in my socket.js file.
Currently, I set io as a global variable so it is accessible from app.js (Global Variable in app.js accessible in routes?), but I understand this is bad practise. Is there a better way to do this (can injection work in this case, as I need to export a variable from the app.js to socket.js rather than the other way round)? Thank you!
app.js
var app = express(),
server = require('http').createServer(app);
//KIV -> io is set as a global variable
io = require('socket.io').listen(server);
require('./socket');
socket.js
io.sockets.on('connection', function(socket) {
//xxx
}