5

I am tying to connect my client to the server socket using socket.io. When I am using http all works fine but when I try to use https the client can't connect.

I try to create the server using http require('https') and using certificates but didn't work.

For now after a few code changes and tests this is how my code is:

Server, index.js

var https = require('https');
var app = express();
var options = {
   key: fs.readFileSync('./server-key.pem'), 
   cert: fs.readFileSync('./server-crt.pem'), 
   ca: fs.readFileSync('./ca-crt.pem'), 
   requestCert: false,
   rejectUnauthorized: false
};


var server = https.createServer(options, app);¡
var io = require('socket.io')(server);

server.listen(3003, function() {
        console.log('server up and running at %s port', 3003);
});


io.on('connection', function(client){
        console.log("NUEVO CLIENTE");
        client.on('event', function(data){});
        client.on('disconnect', function(){});
        client.on('setRoom', function(room) {
                        client.room = room;
                        client.join(room);
        });
        client.on('leaveRroom', function(room) {
                        client.leave(room);
    });

});

The server connection always success using port 3003.

Client

$scope.socket = io.connect('https://socket.softgym.com/', { transports: ['websocket'],rejectUnauthorized: false});
$scope.socket.on('connect_error', function (data) {
                console.log(data);
    });

    $scope.socket.on('message', function(message) {
        $scope.getAttendance();
        $scope.clientDetails(message.user[0]);
    })

Browser logs:

socket.io-client:manager opening https://socket.softgym.com/ +0ms
VM74:6 engine.io-client:socket creating transport "websocket" +5s
VM74:6 engine.io-client:socket setting transport websocket +1ms
VM74:6 socket.io-client:manager connect attempt will timeout after 20000 +4ms
VM74:7 WebSocket connection to 'wss://socket.softgym.com/socket.io/?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 500
r.doOpen @ VM74:7
r.open @ VM74:7
r.open @ VM74:6
r @ VM74:6
r @ VM74:6
r.open.r.connect @ VM74:6
(anonymous) @ VM74:6
VM74:6 engine.io-client:socket socket error {"type":"TransportError","description":{"isTrusted":true}} +502ms
VM74:6 socket.io-client:manager connect_error +501ms
VM74:6 socket.io-client:manager cleanup +0ms
access.js:51 Error: websocket error
    at r.onError (eval at <anonymous> (jquery.min.js:2), <anonymous>:7:8015)
    at WebSocket.ws.onerror (eval at <anonymous> (jquery.min.js:2), <anonymous>:7:23668)
VM74:6 socket.io-client:manager reconnect attempt error +1ms
VM74:6 socket.io-client:manager will wait 5000ms before reconnect attempt +1ms
VM74:6 engine.io-client:socket socket close with reason: "transport error" +4ms
VM74:6 socket.io-client:manager attempting reconnect +5s
VM74:6 socket.io-client:manager readyState closed +1ms

For the ssl I am using load balancer for AWS. enter image description here

This is my apache site:

<VirtualHost *:80>
        ServerName socket.softgym.com
        ServerAlias www.socket.softgym.com
        ServerAdmin webmaster@localhost
        RewriteEngine On
        RewriteCond %{REQUEST_URI} ^/socket.io          [NC]
        RewriteCond %{QUERY_STRING} transport=websocket [NC]
        RewriteRule /(.*) wss://localhost:3003/%1        [P,L]

        ProxyPass /socket.io https://localhost:3003/socket.io
        ProxyPassReverse /socket.io https://localhost:3003/socket.io

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I expect the client connect successfully with the server over https.

D.Pacheco
  • 520
  • 6
  • 24
  • How are you using apache?. The AWS Load balancer is forwarding the request directly to the PORT 3003 of the Node.js APP, or not? – Marcos Casagrande Mar 29 '19 at 22:56
  • @MarcosCasagrande yes I am using apache. The AWS Load balancer is only listening to protocol http and https not requesting directly to PORT 3003 (Edit Post). – D.Pacheco Mar 29 '19 at 23:15
  • So apache is listening on port 80, and then you do proxy pass to your Node.js server?, if so show your apache2 settings. – Marcos Casagrande Mar 29 '19 at 23:16
  • What's the %1 supposed to be a backreference to? There's no capture in the conditions. What does the Apache error log say? – covener Mar 30 '19 at 23:58

1 Answers1

2

Seems like your proxy server does not supports WebScokets upgrade. If you are using apache the configuration is not simple. You will have to install mod_proxy_ws_tunnel module to do this.

Follow this link


Web sockets upgrade is a process that user upgrade from HTTP protocol to WebSckets protocol by sending a upgrade header followed by a three way hand shake. You may find some resources about configuring apache with websocket here


Also if apache server is not required, and you can use another proxy serve. Install nginx and your life will get easier. Then simply add this configuration to your nginx configuration.

location ~* \.io {
  .. your configuration

  proxy_pass http://localhost:3000;
  proxy_redirect off;

  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

Hope that helps.

Janith
  • 2,730
  • 16
  • 26
  • It's works but having apache2 and nginx installed on the same server makes trouble, I uninstalled apache2 and it works correctly. – D.Pacheco Apr 08 '19 at 13:09