title: Accessing Socket.io variable in different files (Express.js)
body: <p>I am trying to make a basic chat application using express.js and socket.io. I have two cofiguration files which are <code>bin/www</code> and <code>app.js.</code> My app starts from <code>bin/www</code>. That is where I create my http server.</p>
  
  <p><code>bin/www</code>:</p>
  
  <pre><code>#!/usr/bin/env node
  
  /**
   * Module dependencies.
   */
  require('babel-register');
  
  var app = require('../app');
  var debug = require('debug')('jukebox-messaging-api:server');
  var http = require('http');
  
  /**
   * Get port from environment and store in Express.
   */
  
  var port = normalizePort(process.env.PORT || '3000');
  app.set('port', port);
  
  /**
   * Create HTTP server.
   */
  
  var server = http.createServer(app);
  var io = require('socket.io').listen(server);
  
  /**
   * Listen on provided port, on all network interfaces.
   */
  
  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);
  
  /**
   * Normalize a port into a number, string, or false.
   */
  
  function normalizePort(val) {
    var port = parseInt(val, 10);
  
    if (isNaN(port)) {
      // named pipe
      return val;
    }
  
    if (port &gt;= 0) {
      // port number
      return port;
    }
  
    return false;
  }
  
  /**
   * Event listener for HTTP server "error" event.
   */
  
  function onError(error) {
    if (error.syscall !== 'listen') {
      throw error;
    }
  
    var bind = typeof port === 'string'
      ? 'Pipe ' + port
      : 'Port ' + port;
  
    // handle specific listen errors with friendly messages
    switch (error.code) {
      case 'EACCES':
        console.error(bind + ' requires elevated privileges');
        process.exit(1);
        break;
      case 'EADDRINUSE':
        console.error(bind + ' is already in use');
        process.exit(1);
        break;
      default:
        throw error;
    }
  }
  
  /**
   * Event listener for HTTP server "listening" event.
   */
  
  function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'
      ? 'pipe ' + addr
      : 'port ' + addr.port;
    debug('Listening on ' + bind);
  }
  
  module.exports = {
      io: io
  };
  </code></pre>
  
  <p>I define my io object in this file and I export it. </p>
  
  <p>When I try to access the io object, for example in my <code>index_controller.js</code></p>
  
  <p><code>routes/index_controller</code>:</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var Server = require('../bin/www');
  
  /* GET home page. */
  router.get('/', function (req, res, next) {
      res.render('index', {title: 'Jukebox Messaging API'});
  });
  
  router.get('/ping', function (req, res, next) {
      res.status(200).json("We are alive!");
  });
  
  router.get('/chats', function (req,res,next) {
     res.render('chat');
  });
  
  Server.io.sockets.on('connection', function (socket) {
      console.log("socket connected");
  });
  
  module.exports = router;
  </code></pre>
  
  <p>In here I try to require the www file and access the io object. But I get this error:</p>
  
  <pre><code>Server.io.sockets.on('connection', function (socket) {
       ^
  
  TypeError: Cannot read property 'sockets' of undefined
      at Object.&lt;anonymous&gt; (index_controller.js:18:1)
  </code></pre>
  
  <p>PS: If you need to see any additional files pls comment, I will happily add them here. Thanks!</p>