title: Socket.io cannot reach module
body: <p>i am trying to create a user module for my chat / video chat application. for this purpose i have the following server file:</p>
  
  <pre><code>    /**
   * 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);
  });
  </code></pre>
  
  <p>As you can see i have my <code>userModule</code> variable outside of the <code>io.on('connection')</code></p>
  
  <p>Simply because i want to store all my users and their details inside that module and not per connection.</p>
  
  <p>However sadly now i am unable to use the module and its functions inside the <code>connection</code> function.</p>
  
  <p>Can anyone tell me how to solve this problem? </p>
  
  <p>im getting: <code>TypeError: userModule.appendUser is not a function</code></p>
  
  <p><strong>userModule</strong></p>
  
  <pre><code>    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);
  };
  </code></pre>
  
  <p><strong>Updated my code:</strong></p>
  
  <pre><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();
  };
  </code></pre>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>    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);
  
  });
  </code></pre>
  
  <p>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.</p>