Skip to content

Websocket support for both native Node.js WebSocket and ws. #1975

@TarikGul

Description

@TarikGul

rel: polkadot-js/api#6079

Node.js v21 introduced a native WebSocket, and with v22 its not behind a flag. This causes issues for node because polkadot-js currently grabs the global websocket over the 'ws' lib if it exists. This can be found below.

export const WebSocket = /*#__PURE__*/ extractGlobal('WebSocket', ws);

export function extractGlobal <N extends GlobalNames, T extends GlobalType<N>> (name: N, fallback: unknown): T {
// Not quite sure why this is here - snuck in with TS 4.7.2 with no real idea
// (as of now) as to why this looks like an "any" when we do cast it to a T
//
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return typeof xglobal[name] === 'undefined'
? fallback as T
: xglobal[name] as T;
}

The problem here is that the native WebSocket, and Ws don't have the same ErrorEvent interface which stops the api from succesfully reconnecting. There is a few options here:

  1. Fix the reconnection on the api level, and give support for both interfaces.

  2. Only expose one WebSocket interface in common in this case ws.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions