0

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.

3
  • Can you post the relevant UserModule.js code? Commented Dec 2, 2015 at 14:28
  • @MattWay Sorry yes it has been posted now Commented Dec 2, 2015 at 14:35
  • what if you require('./costum_modules/UserModule.js')(); ? Commented Dec 2, 2015 at 14:37

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Erm if i move the require inside the io.on(connect) function i am able to call the functions inside the module (such as appendUser) so how can this be correct?
I would have to guess that if you move it inside the connect function you are passing the socket to the require call. Why don't you paste the code that works with the require line inside the connect function.
i have added the code that i have right now im still getting the same error.
Remember, this line var userModule = require('./costum_modules/UserModule.js'); returns a function. You need to run the function to get an actual object, so it should be var userModule = require('./costum_modules/UserModule.js')();. Note the brackets at the end.
Alternatively do what my answer said, and change your export line to module.exports = new UserModule();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.