Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io client behind proxy works fine in browser, not in nodejs application

I'm stuck with a Socket.io exchange. If the javascript code is hosted inside a browser ( Chrome/Firefox) the connection is working with or without proxy in the middle.

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>

<script>
    var socket = io('https://uri', { secure: true, reconnect: true, rejectUnauthorized: false });
    socket.on('connect', function () {
        console.log('Connected to server!');
        socket.emit('register', 'ClientName');
    });
</script>

On the contrary, the same code run on nodejs, using node v10.4.0 and the module "socket.io": "^2.1.1" is working ONLY if the connection is direct.

I've tried to use socket.io-proxy (quite old), but is seems it is not aligned with socket.io-client, and it does not work, or I'm missing something.

It is clear that the "in browser script" can access to the proxy settings/channel...whatever, or to some other setting that node runtime is not aware of.

Thanks for any suggestion.

Lorenzo

like image 596
LorenzoGi Avatar asked Sep 18 '25 17:09

LorenzoGi


1 Answers

Did you manage to solve the problem? If you are behind a simple http(s) proxy you can try with the https-proxy-agent package..

 var HttpsProxyAgent = require('https-proxy-agent');
 let p = 'http://my.proxy.address:8080';
 let agent = new HttpsProxyAgent(p);

 let opts = {
               secure: true,
               rejectUnauthorized: false,
               reconnect: true,
               agent: agent
            };

 let socket = require('socket.io-client').connect('https://my.socket.io.server', opts);
like image 162
Oscar Rentería Quevedo Avatar answered Sep 20 '25 09:09

Oscar Rentería Quevedo