Websocket error – One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0

I have a Node.js server running inside a Docker container. I’m trying to connect to the server using WebSockets from a web browser, but as soon as the connection is established, I get an error:

WebSocket connection to 'wss://domain/api/websockets' failed: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0.

I accessed the Docker container and tried to connect using

wscat --header "Cookie:connect.sid=session-key" -c ws://127.0.0.1:5200/api/websockets

But got the error:

error: Invalid WebSocket frame: RSV1 must be clear

I should note that during the initial connection, I receive valid data packets from the server, but shortly after, the connection drops with the aforementioned error.

Here’s the code of the server:

import WebSocket, { WebSocketServer } from "ws";
import { Server } from "http";
import { sessionParser } from "../passport";
import { User } from "../../share-types";
import { events, EventsList } from "./events";

export default async (server: Server) => {
    const wsServer = new WebSocketServer({
        noServer: true,
        path: '/api/websockets',
    });
    server.on('upgrade', (request: Express.Request, socket, head) => {
        sessionParser(request, {}, () => {
            const user: User = request.session?.passport?.user;
            if (!user?.ID) return socket.destroy();
            wsServer.handleUpgrade(request as any, socket, head, (ws) => {
                wsServer.emit('connection', ws, request, user);
            });
        })
    });


    wsServer.on('connection', function (socket: WebSocket, request: Express.Request, user: User) {

        socket.on('error', console.error);
        socket.on('message', function (message) {
            try {
                message = JSON.parse(message.toString());
                events.emit(EventsList.NEW_WEB_SOCKET_MESSAGE, { user, message });
            } catch { }

        });
    });

    return wsServer;
};

WS package version:
“ws”: “^8.16.0”

What could be the issue?