express.io icon indicating copy to clipboard operation
express.io copied to clipboard

Allowing instances of express.io objects to be used as middleware

Open lededje opened this issue 11 years ago • 5 comments

As detailed in this video for vanilla express http://vimeo.com/56166857, it would be good to be able to pass express.io objects as middleware.

lededje avatar Oct 19 '13 21:10 lededje

I closed it because I thought it has an error I had made, but it turns out it doesn't work after all. When using app.get it works fine, but when you try and tap into the app.io route it complains the object has no such method.

lededje avatar Oct 20 '13 13:10 lededje

Are you sure you passed both the app and the io objects into your route? They should both be defined.

adamboazbecker avatar Oct 21 '13 04:10 adamboazbecker

app.js

var app = require('express.io')()
    , express = require('express')
    , http = require('http')
    , url = require('url')
    , path = require('path')
;

app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'www')));

app.use(require('./homepage'));

app.http().io();

app.listen(80)

homepage.js

var app = module.exports = require('express.io')()
    , express = require('express')
    , http = require('http')
    , url = require('url')
    , path = require('path')
;

app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'www')));

app.http().io(); /// <<<----

app.io.route('customers', function(req){
    console.log('lorem ipsum dolor');
});

This will render the all routes to point at a 'Hello welcome to socket.io'. If you remove the line with the arrow pointing to it, it will render the app.js server correctly, but will fail because hompage.js> app.io is undefined.

lededje avatar Oct 21 '13 10:10 lededje

I can do this:

require('room')(app);

But it doesn't allow you to modify that instance of the app, for example using different cookie secrets for different subdomains.

lededje avatar Oct 21 '13 10:10 lededje

@lededje, I had a similar problem.

Disclaimer: this is just a guess, not guarantee it will work. But, it might give u a path ... or not :smile:

Problem is that when you do a express().http().io() it creates a server based on the current app. But since this app is not the primary, it never listens for anything.

I think, you might be able to solve this modifying the methods below.

But all your internal apps, would still need to have the reference to the main app.

express.application.http = ->
    @server = http.createServer this
    return this

express.application.https = (options) ->
    @server = https.createServer options, this
    return this

to

express.application.http = (param) ->
    @server = http.createServer param or this
    return this

express.application.https = (options, param) ->
    @server = https.createServer options, param or this
    return this

then

app.js

var app = require('express.io')()
    , express = require('express')
    , http = require('http')
    , url = require('url')
    , path = require('path');

app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'www')));

app.use(require('./homepage')(app));

app.listen(80)

homepage.js

module.exports = function(mainApp){
    var express = require('express')
    , http = require('http')
    , url = require('url')
    , path = require('path');
var app = require('express.io')();
app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'www')));

app.http(mainApp).io(); 

app.io.route('customers', function(req){
    console.log('lorem ipsum dolor');
});

};

In your example, it doesnt look like anything would change, but, if you had like below, both your applications could have different cookies/sessions ...

mainApp.use('/first-app', require ...)
mainApp.use('/second-app', require ...)

If you have any results, let me know. I dont know much of the internals of engine.io/socket.io, so this was just a guess, and it may be completely wrong.

lagoasoft-lucasschmidt avatar Nov 16 '13 18:11 lagoasoft-lucasschmidt