3

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

LorenzoGi
  • 256
  • 4
  • 14
  • Does the proxy you are using with your server support webSocket connections? Is it properly configured for webSocket connections? Why are you using a proxy with your server? – jfriend00 Jul 27 '18 at 19:03
  • Hi. The proxy is at enterprise level under IT Dept control. I've described the problem to IT team, and awaiting comments. I've asked if possible to have direct connection for a list of URL. But in general, is it possible to configure the proxy to tunnel websocket connection? Thanks – LorenzoGi Jul 28 '18 at 09:14
  • There are certainly proxies that support webSocket connections, but some do not. It is usually something that can be configured to allow or deny the webSocket connection. – jfriend00 Jul 28 '18 at 10:01
  • What sounds strange to me is the browsers working fine – LorenzoGi Jul 28 '18 at 14:18
  • Now, I see what you mean. You could get a [packet sniffer](https://www.pcwdld.com/best-free-packet-sniffers-and-network-analyzers) and examine the difference in the webSocket connection from the browser vs. the one from the node.js app and see what is different and where it fails. – jfriend00 Jul 28 '18 at 16:24
  • Did you try implementing your own explicit proxy support as shown here [How can I use an http proxy with node-js http client](https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client)? Or perhaps configure built-in proxy support via NPM [How to setup node behind web proxy](https://jjasonclark.com/how-to-setup-node-behind-web-proxy/)? – jfriend00 Jul 28 '18 at 17:53
  • Also, socket.io-proxy is a pretty [small amount of code](https://github.com/stbrenner/socket.io-proxy/blob/master/lib/main.js). You could just step into it and see what it's doing and maybe see what is broken or not configured properly. – jfriend00 Jul 28 '18 at 18:07
  • I'm stuck behind a reply from the proxy : tunneling socket could not be established, statusCode=407. Using various solutions and adding basic authentication. No way. – LorenzoGi Jul 30 '18 at 14:40

1 Answers1

5

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);
  • No. The proxy is not compliant with socket.io. Walkaround is to use a guest network instead, At least this is the IT team answer. Thanks for the suggestions and for asking. Lorenzo – LorenzoGi Jan 23 '19 at 22:00