Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access httpServer instance? #129

Open
yangpu opened this issue Jan 16, 2014 · 13 comments
Open

How to access httpServer instance? #129

yangpu opened this issue Jan 16, 2014 · 13 comments

Comments

@yangpu
Copy link

yangpu commented Jan 16, 2014

I don't find a way to access httpServer instance which is created at boot phase, and this issue has been discussed in issues #87, #88, #125.

Pull request #88 doesn't be merged into 0.4.x.

It's request to expose httpServer instance for initializers to support socket.io listen to the same port as http service.

@throrin19
Copy link

+1

1 similar comment
@FredJacquemin
Copy link

+1

@throrin19
Copy link

I found a solution.

In your initializer folder, add this file : 00_server.js :

module.exports = function() {
    var app = this.express;
    this.server = require('http').createServer(app);
};

And XX_socketio.js (replace XX by correct number of initializer) :

var socketio = require('socket.io');

module.exports = function() {
    this.io = socketio.listen(this.server);

    // socket.io config and routes
};

@FredJacquemin
Copy link

Cool! would this work with an older version? (0.3.x?)

@throrin19
Copy link

i try this with latest locomotive version. Sorry

@throrin19
Copy link

It seems that it does not work. Indeed, the server created here is different from the locomotive.

@throrin19
Copy link

Ok, after research, locomotive initialize the http server after all initializers and, in boot function it is not possible to retrieve the server object. In this case, it's impossible to use socket.io with locomotive 0.4.x

@FredJacquemin
Copy link

Good to know! Thanks for sharing :)

@throrin19
Copy link

After others tests, i finally find this and launch socket.io 💃

In your new server.js, remove the line for init httpServer :

app.phase(locomotive.boot.httpServer({ address: address, port: port }));

In initializers/00_server.js :

module.exports = function(done) {
    var app = this.express,
        port = process.env.PORT || 3000,
        address = '0.0.0.0';

    if(this.env == 'test'){
        port = 3030;
    }

    var self = this;
    require('http').createServer(app).listen(port, address, function() {
        var addr = this.address();
        console.log('listening on %s:%d', addr.address, addr.port);
        self.server = this;
        done();
    });
};

And in initializers/XX_socketio.js :

var socketio = require('socket.io');

module.exports = function() {
    this.io = socketio.listen(this.server);

    // socket.io config and routes
};

After that, it works fine ;)

@FredJacquemin
Copy link

This deserves a bookmark :) Thanks again for sharing.

@OtaK
Copy link

OtaK commented Feb 24, 2014

Hi there, I have made a plugin for locomotive, to use Primus (abstraction layer over many websockets engines). https://github.com/OtaK/locomotive-primus

It does what you're asking (exposing http server in app instance). 👍 :-)

@FredJacquemin
Copy link

will definitively give this a try asap... wow, nice idea!

@OtaK
Copy link

OtaK commented Feb 24, 2014

Just for the record, Jared, the author of Locomotive knows about it and featured it here : https://github.com/jaredhanson/locomotive/wiki/Modules

Don't hesitate if you have any questions about it !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants