wzrdtales / socket-io-sticky-session

Sticky session balancer with layer 4 capabilities, based on a `cluster` module
72 stars 21 forks source link

can u please give working example with socketio ? #14

Open emps opened 8 years ago

emps commented 8 years ago

im tring make module work with meanio tried many ways even changed setupSlave callback to be promise cause meanio server start isnot sync and so i rechanged the server.js of meanio to (server starts but client sockets cant connect to the server )

var mean = require('meanio'),
    cluster = require('cluster');
var sticky = require('socketio-sticky-session');
var options = {
    proxy: true, //activate layer 4 patching
    header: 'x-forwarded-for', //provide here your header containing the users ip
    // num: 2 //count of processes to create, defaults to maximum if omitted
}
sticky(options, function() {
    // This code will be executed only in slave workers
    return new Promise(function(resolve, reject) {
        var mean = require('meanio');
        mean.serve({
            workerid: cluster.worker.id /* more options placeholder*/
        }, function(app) {
            var config = require('meanio').loadConfig(),
                socketio = require('socket.io'),
                socketioJwt = require('socketio-jwt');
            // var config = app.config.clean;
            // var port = config.https && config.https.port ? config.https.port : config.http.port;
            // console.log('Mean app started on port ' + port + ' (' + process.env.NODE_ENV + ') cluster.worker.id:');
            var io = socketio.listen(app);
            io.use(socketioJwt.authorize({
                secret: config.secret,
                handshake: true
            }));
            io.on('connection', function(socket) {
                if (socket.decoded_token) socket.user = JSON.parse(decodeURI(socket.decoded_token));
            });
            resolve(app);
        });
    })
});

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/29816731-can-u-please-give-working-example-with-socketio?utm_campaign=plugin&utm_content=tracker%2F10408799&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F10408799&utm_medium=issues&utm_source=github).
wzrdtales commented 8 years ago

Could you may be deliver a bit more of information. Actually I have nothing and would need to reproduce this all myself. Do you have any error messages and so on?

I do not use mean.io, so please provide a complete gist or git repo that is reproducing your problem.

emps commented 8 years ago

well i get Status Code:404 Not Found

Request URL:http://localhost:3010/socket.io/?token=eyJhbGciO...xK8&EIO=3&transport=polling&t=L8taw-Q

emps commented 8 years ago

i can see also in ur examples

  io.listen(server);
  return server;

do u have any example what to do with the io ?> cause we return server and looks like io dies there

wzrdtales commented 8 years ago

This is not that helpful...

This module needs to overwrite the listen function of the webserver, I guess "serve" already starts the webserver, this is definitely wrong.

emps commented 8 years ago

yeh it starts it

wzrdtales commented 8 years ago

Then you probably need to stop using the serve method and use express directly.

emps commented 8 years ago

but then i need stop using all the engine of meanio ? i wish some how include ur module with meanio

emps commented 8 years ago

btw app works only sockets cant connect

wzrdtales commented 8 years ago

there might be a way to use meanio with this module, but it does not seem that there is any proper documentation for mean io

emps commented 8 years ago

and if mean.serve creates server its same like http.createServer in ur example

emps commented 8 years ago

no much docs i just read the code of engine to understand a bit big code

wzrdtales commented 8 years ago

If there aren't that much docs it gets even harder to reproduce this and costs way to much time...

Mean uses express, so I expect that it should be handled the same way how one would handle it with express. The question is how to reach express, we don't want that serve thingy if this already binds to ports.

wzrdtales commented 8 years ago

https://github.com/linnovate/meanio/blob/8d587fa91d7472737af7d2801e5e9331d438d5c6/lib/core_modules/server/ExpressEngine.js#L126-L142

It always binds to a port, thus my best advice is to you to create your socket io application apart from your mean.io project. You could also ask the devs if there is a way to get the server without binding to a port how to access the express component manually start the listening.

It is just common that projects like mean.io tend to build "convenience" wrapper and thus build things in ways that are highly incompatible... If you hear something else or get information how to access the express component and disable mean.io to bind per default you can comeback and we will get this working.

timelf123 commented 8 years ago

@emps see https://github.com/linnovate/mean/pull/1472 and https://github.com/linnovate/meanio/pull/93

emps commented 8 years ago

yes thanks @timelf123 i also added there some *fix