Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Socket.io cannot reach module

i am trying to create a user module for my chat / video chat application. for this purpose i have the following server file:

    /**
 * Created by root on 3/13/15.
 */
var multer = require('multer');
var express = require('express');
bodyParser = require('body-parser');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cookieParser = require('socket.io-cookie-parser');
var ExpressPeerServer = require('peer').ExpressPeerServer;

var userModule = require('./costum_modules/UserModule.js');
io.on('connection', function (socket) {
    var my_user = userList.id;
    socket.on('userData', function (userDetails) {
        userModule.appendUser(userDetails);
    });


    var chatModule = require('./costum_modules/ChatModule.js')(socket, userModule);
    var cacheModule = require('./costum_modules/CacheModule.js')(socket, userModule);
    var notificationModule = require('./costum_modules/NotificationModule')(socket, sequelize, userList);

});


app.use('/peerjs', ExpressPeerServer(http, options));

var listenTo = require('./port.json')["port"];

http.listen(listenTo, function () {
    console.log('listening on *:' + listenTo);
});

As you can see i have my userModule variable outside of the io.on('connection')

Simply because i want to store all my users and their details inside that module and not per connection.

However sadly now i am unable to use the module and its functions inside the connection function.

Can anyone tell me how to solve this problem?

im getting: TypeError: userModule.appendUser is not a function

userModule

    var UserModule = function (socket) {
    var activeUsers = [];

    this.appendUser = function (userDetails) {
        activeUsers.push(userDetails);
    };

    this.getUserById = function (id) {
        activeUsers.forEach(function (y) {
            if (y.id == id) {
                return y;
            }
        });
    }

};

module.exports = function (socket) {
    return new UserModule(socket);
};

Updated my code:

    var UserModule = function () {
    var activeUsers = [];

    this.appendUser = function (userDetails) {
        activeUsers.push(userDetails);
    };

    this.getUserById = function (id) {
        activeUsers.forEach(function (y) {
            if (y.id == id) {
                return y;
            }
        });
    }

};

module.exports = function () {
    return new UserModule();
};

server.js

    var userModule = require('./costum_modules/UserModule.js');
io.on('connection', function (socket) {
    var my_user = userList.id;
    socket.on('userData', function (userDetails) {
        userModule.appendUser(userDetails);
    });


    var chatModule = require('./costum_modules/ChatModule.js')(socket, userModule);
    var cacheModule = require('./costum_modules/CacheModule.js')(socket, userModule);
    var notificationModule = require('./costum_modules/NotificationModule')(socket, sequelize, userModule);

});

if i instead put the require of the module inside the io.on('connect') i am able to access the functions just fine. However this is not what i want since it will intialize it for each of the connections made to my server.

Upvotes: 0

Views: 70

Answers (1)

Matt Way
Matt Way

Reputation: 33171

The reason why you are getting that error is because this line:

var userModule = require('./costum_modules/UserModule.js');

does not return a UserModule, but rather returns a function that accepts a socket:

function (socket) {
    return new UserModule(socket);
};

In order to get a valid UserModule object that has an appropriate appendUser function, you need to do something like this:

// provide the module with a socket
var userModule = require('./costum_modules/UserModule.js')(socket);

But I suspect that you want to create one before a socket is created. If you only want to create a single user module to work with, then change the UserModule.js export type to:

module.exports = new UserModule();

and remove the socket from the UserModule constructor.

Upvotes: 2

Related Questions