Yann LM
Yann LM

Reputation: 71

Socket Io Error on connection (Error during WebSocket handshake: Unexpected response code: 400)

I have a problem with the client/server side of Socket.io, I have a VPS and I recently used 'Certbot' to automatically enable HTTPS on my domain. Well, I use apache on Debian jessie and use a URL like 'api.domaine.com' with https enabled from Certbot, I'm using Node.js on this one with Express and Socket.io server side.

I'm currently making a chrome extension with socket.io which is client side.

Here is my io configuration from the server :

let PORT = process.env.PORT || 1337;
let server = app.listen(PORT);
let io = require('socket.io').listen(server);

io.sockets.on('connection', function(socket) {
  socket.on('joinRoom', function(room) {
    socket.join(room);
    console.log(io.sockets.adapter.rooms);
    socket.emit('joined', room);
  });
});

And my connection line from client in the background.js:

let socket = io.connect('https://api.domain.com/');

My problem is that the browser's console (Chrome Dev Tool) returns this:

WebSocket connection to 'wss://api.domain.com/socket.io/?EIO=3&transport=websocket&sid=T4dxwvDQUX8PvS86AAAF' failed: Error during WebSocket handshake: Unexpected response code: 400

EDIT: My Virtualhost configuration for the SSL:

<IfModule mod_ssl.c>
<VirtualHost *:443>
   ServerName api.domain.com

   ProxyPass / http://0.0.0.0:1337/
   ProxyPassReverse / http://0.0.0.0:1337/
   ProxyPreserveHost On
SSLCertificateFile /etc/letsencrypt/live/api.domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Upvotes: 1

Views: 2984

Answers (1)

Yann LM
Yann LM

Reputation: 71

I solved it by making some changes and add proxy configuration in the Apache Virtualhost SSL configuration of my domain :

<IfModule mod_ssl.c>
<VirtualHost *:443>
   ServerName api.domain.com

   SSLEngine on
   SSLProxyEngine On
   ProxyRequests Off

   ProxyPass / http://0.0.0.0:1337/
   ProxyPassReverse / http://0.0.0.0:1337/
   ProxyPreserveHost On

   SSLCertificateFile /etc/letsencrypt/live/api.domain.com/fullchain.pem
   SSLCertificateKeyFile /etc/letsencrypt/live/api.domain.com/privkey.pem
   Include /etc/letsencrypt/options-ssl-apache.conf

   RewriteEngine on
   RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
   RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
   RewriteRule .* ws://0.0.0.0:1337%{REQUEST_URI} [P]
</VirtualHost>
</IfModule>

Upvotes: 2

Related Questions