title: Node.js - How to emit socket.io events from outside app.js?
body: <p>i've been trying to organize my app in a simple yet effective way, other than processing all my events at the bottom of the app.js file. </p>
  
  <p>i have to say, my apps are simple, i'm not a pro developper, so i'm looking for something simple. Other solutions i've found were all too complicated</p>
  
  <p>my project's folder structure is the following :</p>
  
  <pre><code>node_modules
  public
  models
   ----&gt; User.js
  app.js
  </code></pre>
  
  <p>inside User.js, I have the following code : </p>
  
  <pre><code>var mongoose = require('mongoose'),
      Schema = mongoose.Schema,
      bcrypt = require('bcrypt-nodejs');
  
  var User = function(){
  
      var userSchema = new Schema({
        email : {type: mongoose.SchemaTypes.Email,
        required: true,
        index: { unique: true }
      }
      });
  
      var userModel = mongoose.Model('User', userSchema);
  
      this.create = function(user_infos){
  
          var newUSer = new userModel({
                  password:bcrypt.hashSync(data.user_infos.password),
                  email:data.user_infos.email
              });
  
          newUser.save(function(err){
              if(err){ //...}
              else{
                  socket.emit('user created', {id:user._id.toString()});
              }
          });
      }
  });
  
  module.exports = User;
  </code></pre>
  
  <p>And my app.js looks like this:</p>
  
  <pre><code>var express = require('express'),
      app = express(),
      server = require('http').createServer(app),
      io = require('socket.io').listen(server),
      mongo = require('mongodb'),
      mongoose = require('mongoose'),
      bcrypt = require('bcrypt-nodejs'),
      User = require('./models/User.js'),
      Event = require('./models/Event.js')
  
  
      // ========= CONFIG ================
      app.set('port', process.env.PORT || 1337);
      app.use(express.static(__dirname + '/public'));
      io.set('log level', 1);
  
  
      //========= ROUTES ================
      app.get('/home', function(req,res){
          res.sendfile( __dirname + '/index.html');
      });
      app.get('*', function(req,res){
          res.redirect('/home');
      });
  
  
      server.listen(app.get('port'), function() {
      console.log("Listening on " + app.get('port'));
      });
  
      var MONGOHQ_URL="mongodb://........."
  
      mongoose.connect(MONGOHQ_URL);
      var db = mongoose.connection;
      db.on('error', console.error.bind(console, 'connection error:'));
      db.once('open', function callback () {
        console.log("Connection to the database succes!");
  
          io.sockets.on('connection', function(socket){
          console.log('Client connected');
  
          global.socket = socket;
  
          socket.on('try me!', function(data){
  
                  User.create(data);
  
              });
          });
  
      });
  </code></pre>
  
  <p>I don't pretend that this is a good way to structure things, but that's something I can understand. </p>
  
  <p>When the event is triggered clientside, the server stops and Chrome Dev Tools prints out: </p>
  
  <pre><code>GET http://localhost:1337/socket.io/1/?t=1397151884883 net::ERR_CONNECTION_REFUSED 
  </code></pre>
  
  <p>Any thoughts ?</p>