title: Socket.io in Different Files
body: <p>To use the io object in different files I am using the approach below.. I seems like I am not getting compile errors. So io is "defined" in the users.js file.. But I dont seem to be emmiting any event. Does anyone see the problem?</p>
  
  <p>app.js</p>
  
  <pre><code>var app = require('express')()
    , server = require('http').createServer(app)
  
  global = require('./global.js');
  global.io = require('socket.io').listen(server);
  global.io.configure(function () { 
    global.io.set("transports", ["xhr-polling"]); 
    global.io.set("polling duration", 10); 
  });
  // express settings
  require('./config/express')(app, config, passport)
  
  require('./config/passport')(passport, config, env)
  
  // Bootstrap routes
  require('./config/routes')(app,passport)
  
  var port = process.env.PORT || 3000
  server.listen(port)
  </code></pre>
  
  <p>global.js</p>
  
  <pre><code>var Global = {
      io : { }
  };    
  module.exports = Global;
  </code></pre>
  
  <p>routes.js</p>
  
  <pre><code>var users = require('../app/controllers/users')
  
  module.exports = function (app, passport) {
      app.post('/incomingFake', users.getFake);
  }
  </code></pre>
  
  <p>users.js</p>
  
  <pre><code>var global = require('../../global.js')
  
  exports.getFake = function(req, res){
      global.io.sockets.on('connection', function(socket) {
          socket.broadcast.emit('email', req.body);
      });
  
      User.findAndStoreEmail(senderEmail, email ,function(){
          res.writeHead(200, {'content-type': 'text/plain'})
          res.end('Message Received. Thanks!\r\n')
      })
  
  
  }
  </code></pre>
  
  <p>and I try to receive this in a backbone collection in the frontend</p>
  
  <pre><code>define([
      'jquery',
      'underscore',
      'backbone',
      'Email',
      'marionette',
      'socketio'
  ], function ($, _, Backbone, emailModel, Marionette, io) {
      'use strict';
  
      var Emails = Backbone.Collection.extend({
  
          url : '/emails',
          model : emailModel,
  
          initialize : function() {
              var socket = io.connect('http://localhost:3000');
               socket.on('email', function (data) {
                  console.log("i got the socket connection");
                  console.log(data);
              });
          }
  
      });
  
      return Emails;
  });
  </code></pre>
  
  <p>edit:</p>
  
  <p>I realized something! I think this might help a lot to resolve the problem, I actually receive the connection event and the socket emits something but this all happens with the page load.. I want this event to be emitted only the 'getFake' function is called</p>