Search code examples
nginxwebsocketsocket.ioengine.io

WebSocket is closed before the connection is established - Socket.io usage error on production


I have setup a socket io web application, it works fine on local system but on production server, it does't work and I see these logs

Logs on client side

WebSocket connection to 'wss://mysite.example.com/socket.io/?EIO=3&transport=websocket&sid=NedsfsdfngOnAdfdVAADc' failed: WebSocket is closed before the connection is established.

Logs on server side

101.254.47.113 - - [02/Jul/2020:19:45:51 +0000] "GET /socket.io/?EIO=3&transport=polling&t=NCJljKs&sid=1FddfgdfGFsfsgYkAAIT HTTP/1.1" 200 915 "https://mysite.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
101.254.47.113 - - [02/Jul/2020:19:45:52 +0000] "POST /socket.io/?EIO=3&transport=polling&t=NCJljPV&sid=1FddfgdfGFsfsgYkAAIT HTTP/1.1" 200 2 "https://mysite.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
101.254.47.113 - - [02/Jul/2020:19:45:52 +0000] "GET /socket.io/?EIO=3&transport=polling&t=NCH45_E HTTP/1.1" 200 103 "https://mysite.example.com/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
101.254.47.113 - - [02/Jul/2020:19:45:53 +0000] "GET /socket.io/?EIO=3&transport=polling&t=NCH464Z&sid=JtMPdsfdsjzfdsfd43AAIU HTTP/1.1" 200 915 "https://mysite.example.com/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"                                                                                                                

I have these nginx configuration (I have redirected non-ssl traffic to ssl port 443

server
{ 
    listen 443 ssl; # managed by Certbot
    server_name mysite.example.com;

    ssl_certificate /etc/letsencrypt/live/mysite.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    client_max_body_size 100M;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        client_max_body_size 100M;
    }
}

server {
        listen 80;
        listen [::]:80;
        server_name mysite.example.com;
        return 301 https://$server_name$request_uri;
}

Solution

  • The issue was with version mismatch of websocket client and server. I updated the client side with the right version of websocket and the issue was resolved. Although I was working with code written by someone else, I should have checked this earlier and should have been debugging patiently rather than jumping to conclusions.