Answer the question
In order to leave comments, you need to log in
How to use socket.io instance in Node.js controllers?
If I want to use a socket.io instance in a controller, is global.io = io really awful? If so, how can I reach io from another of my controllers?
index.js
const server = app.listen(3000, '127.0.0.1', () => {
console.log("Node.js listening on port 3000");
});
// socket
const io = require('socket.io')(server);
const sharedsession = require("express-socket.io-session");
global.io = io;
io.use(sharedsession(session));
require('./socket/socket')(io);
module.exports = (io) => {
io.on('connection', socket => {
console.log('a user connected');
socket.on("event", () => {
});
});
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question