Monday, 13 March 2017

Using socket.io in routes other than server/app.js

Hi I am trying to use socket.io in a file other than my main server.js file where I first set it up like so:

const app = require('express')();
const http = require('http').createServer(app);
const mongoose = require('mongoose');
const io = require('socket.io')(http);

io.on('connection', (socket) => {
    console.log('a user connected');
    socket.on('disconnect', () => {
        console.log('user disconnected');
    });
});

Since I am setting it up in my server.js file I have to listen for events in this file and therefore the callback function will only have access to the scope of this file. However I need to use this in another file in my routes that I have set up like so for example:

app.use('/', home);

I have read both this and this and neither one has helped me.

Thanks.



via JohnNoob

No comments:

Post a Comment