0

I've been bouncing back and forth between socket.io and express.io - but settled for socket.io with Express 4, as I would like to use Namespaces.

I have worked on some examples of having an Express 4 Server using Socket.io - but most examples are based on one file with everything in it. I am trying to separate all my code to make it easier but I am at a loss as to how to add Socket.io (or where).

I have index.js which uses Cluster and basically calls server.js:

var server = require( "./server.js" );
var cluster = require('cluster');

var webApp={
    run: function(){
      console.log('Starting: Server');
      server.listen();
    }
};

if(cluster.isMaster){
    cluster.fork();
    cluster.on('exit',function(worker){
      console.log('Worker ' + worker.id + ' died..');
      setTimeout( function () { cluster.fork(); }, 1000 );
    });
} else{
    try {
        webApp.run();
    }
    catch(e)
    {
        console.log(e);
        process.exit(1);
    }
    process.on('uncaughtException', function(err){
        console.log(err);
        process.exit(1);
    });
    process.on( 'SIGINT', function () {
      console.log( "\n SIGINT (Crtl-C)" );
        //Kill worker
        cluster.disconnect();
        process.exit(1);
    });
}

This then calls the server.js file:

var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var config = require('./config/config.js');
var router = require('./routes');

var Server = Object.subClass({
  /**
     * Constructor
     */
  init:function(){
    this.appServer = express();
    var that = this;
    var appServer = this.appServer;

    appServer.use(express.static(__dirname + '/public'));
    appServer.set('views', path.join(__dirname, 'views'));
    appServer.set('view engine', 'ejs');
    appServer.use(bodyParser.urlencoded({ extended: true }));
    appServer.use(bodyParser.json());
    appServer.get('/',router.root);
  },
  /**
    * Listener HTTP
    */
  listen:function(){
    var port = config.rest.port;
    console.log(':: on port:' + port);
    this.appServer.listen(port);
  }
});

module.exports = new Server();

I am only having one 'route', which is the '/' and is defined in routes.js file. The page loads fine but where do I add the server side socket.io? and do I add any socket.io namespace definitions in the routes.js file or in the javascript of the page being loaded?

There are so many ways of using sockets that I can't seem to work out the best approach for my multi-file approach.

Any help would be brilliant as I seem to be going in circles.

Enjoy our Saturday :)

Thanks again.

Pandafinity
  • 713
  • 2
  • 7
  • 19
  • Sorry, but I don't really understand what question you're asking? Are you just trying to figure out how to split your code into multiple files? Or is there something more involved to the question? And, where is your socket.io code? I put mine right after my `app.listen()` call that starts my server. – jfriend00 Oct 04 '14 at 16:54
  • Hi. Basically yes. I am trying to understand how to add or wrap the socket.io code into what I have. I've seen so many ways of implementing sockets that I'm not sure which style or approach to use. Would you have a file of your socket def of namespaces etc in a separate file? With the routes? Thanks – Pandafinity Oct 04 '14 at 20:01
  • You should create a new file if you have a piece of reusable code that all goes together and might be used in other projects or if you have a large enough piece of related code that it makes sense to break it out into its own file. `socket.io` creates no unique issues in this regard. – jfriend00 Oct 04 '14 at 20:17
  • Definitely something I want to do (separate file), but being honest none of the code I've tried even creates a socket.io connection. – Pandafinity Oct 04 '14 at 20:37
  • Get the connection working with the code in your main file and then, after it's working, worry about how to move it to a new file. One problem at a time. – jfriend00 Oct 04 '14 at 20:47
  • That's what I'm trying to do............ – Pandafinity Oct 05 '14 at 09:24
  • Then why haven't you shown the socket.io code you're using? Kind of hard for us to know what's wrong with something you haven't shown us. – jfriend00 Oct 05 '14 at 11:22
  • I appreciate that.... But all the code I tried would not work with the cluster/worker approach I was using. Hence wondering if there was a Best Practice approach.....but I guess its horses for courses. I've decided I'm going to scrap it and use SocketCluster - but thank you for taking the time to help. Hope you enjoy your Sunday. – Pandafinity Oct 05 '14 at 12:40
  • Next time, please make sure your question actually asks the question you want answered. There is absolutely nothing in your written question about what you think was eventually the answer (cluster-compatible sockets). To me it's like you went off in a totally different direction from what you actually asked. – jfriend00 Oct 05 '14 at 17:21
  • "There are so many ways of using sockets that I can't seem to work out the best approach for my multi-file approach." Was in my first question - which basically summed up my problem - and as such a question as regards the best approach for a cluster/worker approach. Sorry it wasn't clear... – Pandafinity Oct 05 '14 at 19:31
  • Does multi-file == cluster in your question? I thought you were asking about how to structure the different modules of your code in different files. Guess I didn't understand your question at all. – jfriend00 Oct 05 '14 at 19:53
  • nme talking about index.js & server.js(worker) both shown - and mentioning routes.js and talking about socket definitions in separate files :) - that would be multiple files - just the context was in reference to a cluster/worker approach. Don't worry it was a Sunday.... have a good week. – Pandafinity Oct 05 '14 at 22:11

1 Answers1

1

I've spent the morning looking at the Cluster/Worker approach and decided to use 'SocketCluster' as it seems to do what I need.

Enjoy your Sunday

Pandafinity
  • 713
  • 2
  • 7
  • 19