'"Error: Invalid WebSocket frame: RSV1 must be clear" while using Socket.IO

What might be the problem for which, I'm getting this error "Error: Invalid WebSocket frame: RSV1 must be clear"?

I'm using the socket.io package to connect to Node.js with following code.

let app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http, /*options*/);

io.on('connection', function (socket) {
  console.log('a user connected');
  socket.conn.on('upgrade', ()=>{
    const upgradedTransport = socket.conn.transport.name;    
  });

socket.conn.on('upgrade', ()=>{
    const upgradedTransport = socket.conn.transport.name;    
  });
socket.on('create or join', function (room) {...});
socket.on('ready', function (room) {...});
socket.on('candidate', function (event){...});
.....
.....
socket.on('hangup', function (room) {    
    socket.leave(room);
    socket.emit('hangup', room);
   
  });
});
});

Here is my Nginx configuration,

map $http_upgrade $connection_upgrade {
default upgrade;
    '' close;
}

upstream websocket {
    server 127.0.0.1:1337;
}

server {
        server_name  mydomain.subdomain.com;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;
        underscores_in_headers on;

  location /socket.io/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:1337;
        # this magic is needed for WebSocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        }

location / {
        proxy_pass http://127.0.0.1:8094;
        }


    listen [::]:443 ssl ipv6only=on; 
    listen 443 ssl; 
    ssl_certificate ...
    ssl_certificate_key /etc/...; 
    include /etc/...;
    ssl_dhparam /etc/...;
}

 server {
    if ($host = mydomain.subdomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen       80;
        listen       [::]:80;
        server_name  mydomain.subdomain.com;
    return 404; 


}


While requesting from the Postman, Handshake details are as below,

Handshake Details

Request URL: https://mydomain.subdomain.com/socket.io
Request Method: GET
Status Code: 101 Switching Protocols
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: 3DlYbOFVRGepjPrOd92Zew==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: mydomain.subdomain.com

Also the response header,

Server: nginx/1.14.0 (Ubuntu)
Date: Tue, 10 May 2022 12:50:41 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: FB8P5FMN8gPIsHjfRrrIoe+9+OI=

What might be the problem for which, I'm getting this error "Error: Invalid WebSocket frame: RSV1 must be clear"? Thank you for helping me out.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source