I am probably missing something, but I cannot get my head around on how to properly reference a socket (created with socket.io) in another file. I have a server.js file:
const app = express();
const io = require('socket.io')(server, {
cors: {
origin: 'http://localhost:8081',
methods: ['GET', 'POST'],
allowedHeaders: ['my-custom-header'],
credentials: true,
},
});
require('./controllers/buildXLS.js')(io);
I want to access io in another file ('./controllers/buildXLS.js'):
module.exports = io => {
io.on('connection', client => {
client.on('subscribeToProgress', () => {
console.log('client is subscribing to progress');
});
});
};
Now this works fine with module.exports, but a exports.io = io =>... crashes the server (require is not a function).
I do have several other exports in buildXLS.js, which will be destroyed by module.exports.
Is there any way around that? I need io in buildXLS.js, as it will emit progress updates.
Thanks