How to use socket io instance on multiple files in express.js app

In my express app, generated with express-generator, I want to use the io of socket.io in some other controller files to emit data to client sockets. My approach is below, but I get the following error with that. It would be a great favor if someone can help me in this case.

(node:11376) UnhandledPromiseRejectionWarning: TypeError: io.emit is not a function
at F:\backend\controllers\LessonController.js:169:9

In the express apps, generated by express-generator, the process of creating the server happens in the /bin/www.js. I tried importing the io instance from there and use it in some other file, but it didn’t work.

bin/www.js

#!/usr/bin/env node

var app = require('../app');
var debug = require('debug')('backend:server');
var http = require('http');

var port = normalizePort(process.env.PORT || '8080');
app.set('port', port);

var server = http.createServer(app);
const io = require('socket.io')(server);

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

// several other functions are omitted for brevity

module.exports = io;

LessonController.js

const Lesson = require('../models/Lesson');
const Course = require('../models/Course');
const User = require('../models/User');
const io = require('../bin/www')
var _ = require('lodash');

module.exports = {
    addComment: async (lessonId, userId, content, callback) => {
        const newData = {
            comments: {
                user: userId,
                content: content,
            },
        };

        Lesson.findOneAndUpdate({ _id: lessonId }, { $push: newData }, {new: true})
        .exec()
        .then(
            function (data) {
                if (data) {
                    io.emit("comment_"+lessonId,data)
                    callback(null, data);
                } else if (err) {
                    callback(err, null);
                }
            }
        )
    }
};

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You can try to export the socket.io instance to the global level and access that as needed.

My project was also created with express-generator, therefore, follows the same template.

In my project, I would like to count the current number of active users in home page.

Here is an example:

bin/www

#!/usr/bin/env node
const app = require('../app');
const http = require('http').Server(app);
const io = require('socket.io')(http)
http.listen(process.env.PORT);
io.on('connection', (socket) => {    
    const qtd = socket.client.conn.server.clientsCount;
    io.emit('novaconexao', qtd);
    socket.on('disconnect', () => {
        io.emit('disconnecteduser', qtd - 1);
    });
});
app.set('socketio', io);//here you export my socket.io to a global       

console.log('Microsservice login listening at http://localhost:%s', process.env.PORT);

server/index.js

const router = require('express').Router();
router.get('/', (req, res) => {
    const io = req.app.get('socketio'); //Here you use the exported socketio module
    console.log(io.client.conn.server.clientsCount)
    io.emit('new-user', {qtd: io.client.conn.server.clientsCount})
    res.status(200).json({ msg: 'server up and running' });
})
module.exports = router;

Following this strategy, you can use socketio in any route in your application.

Solution 2

Here is a solution

Create a module io.js

const sio = require('socket.io');

let io = null;
module.exports = {
    //Initialize the socket server
    initialize: function(httpServer) {
        io = sio(httpServer);
        io.on('connection', function(socket) {
            console.log('New client connected with id = ', socket.id);
            socket.on('disconnect', function(reason) {
                console.log('A client disconnected with id = ', socket.id, " reason ==> ", reason);
            });
        });

    },
    //return the io instance
    getInstance: function() {
        return io;
    }
}

In bin/www.js

var server = http.createServer(app);
require('path_to_io_js/io').initialize(server);

In your controllers / LessonController.js

//require the io module
const socket = require('path_to_io_js/io');
module.exports = {
    addComment: async (lessonId, userId, content, callback) => {
        const newData = { comments: { user: userId, content: content, }, };
        Lesson.findOneAndUpdate({ _id: lessonId }, { $push: newData }, { new: true })
            .exec().then(function (data) {
                if (data) {
                    //get the io instance
                    const io = socket.getInstance();
                    io.emit("comment_" + lessonId, data)
                }
                callback(null, data);
            }).catch(err => {
                callback(err);
            })
    }
};

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply