Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to websocket using ssl and apache

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.

like image 238
D.Pacheco Avatar asked Oct 17 '22 05:10

D.Pacheco


1 Answers

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.

like image 87
Janith Avatar answered Nov 13 '22 01:11

Janith