I have a project set up where in my bin/www file I have my node server set up with the following line which works:
const server = http.createServer(app);
I am working on setting up socket.io so I would like to make another file called socket.js and in there do all the socket.io set up but I need to be able to get access to my server variable. I tried to export my server variable like this:
module.exports = server;
and then use it in my new file like this:
const server = require("./bin/www");
const io = require('socket.io')(server); //server doesn't seem to be working here
io.on("connection", socket => {
socket.on('sendMessage', data => {
socket.join(socket.id, () => {
console.log('Joined group');
io.sockets.in(socket.id).emit('sendMessage', data)
});
});
});
But it doesn't look like this is setting up the socket on the server as I am getting the following error:
GET /socket.io/?EIO=3&transport=polling&t=N1HOkXK
which happens when it isn't set up correctly.