2

I have a socket.io server with code like that:

var sticky = require('sticky-session'); sticky(function() {
    var app = require('http').createServer(handler);

    function handler (req, res) {
        res.writeHead(200, {
            "Access-Control-Allow-Origin": "*"
        });

        return res.end('pong');
    }

    var io = require('socket.io')(app, {'origins' : '*' + config.application.hostname + '*:*'});

    const newmessagesConnections = io.of('/new-messages');
    newmessagesConnections.on('connection', function (client) {
    .....
    });

    const callingsConnections = io.of('/callings')
    callingsConnections.on('connection', function (client) {
    .....
    });

    return app;
}).listen(config.application.port, function() {
    console.log('server started on ' + config.application.port + 'port');
});

It works well with WS protocol, but I want to support WSS protocol too. Is it possible user both WS ans WSS protocol when I use sticky-session?

From sticky-session doc:

Sticky-sessions module is balancing requests using their IP address. Thus client will always connect to same worker server, and socket.io will work as expected, but on multiple processes!

But I want to do possibility to have open connections via WS and WSS and I can't understand who to update my server to keep WS and WSS connections.

Dmitro
  • 1,489
  • 4
  • 22
  • 40

1 Answers1

2

In your client code, when you create the socket connection, try this:

if(location.protocol==='https:'){
    socket = io.connect( 'wss://'  + location.hostname + '/', {    
        //options here like:
        'forceNew':true
    });
}
else{
    socket = io.connect( 'ws://'  + location.hostname + '/', {
        //options here like:
        'forceNew':true
    });
}
Blubberguy22
  • 1,344
  • 1
  • 17
  • 29