perspective-dev/perspective · error

Already connected

Error message

Already connected ${ws.readyState}

What it means

Guard in the websocket client's connect(): if a previous WebSocket for this client is still CONNECTING or OPEN, a re-connect attempt is refused with this warning instead of opening a second socket. It fires when callers invoke connect (e.g. after an error handler) while the existing connection is still usable or being established.

Solutions

  1. Avoid calling connect while an existing socket is OPEN or CONNECTING; await its completion first
  2. Close the current websocket explicitly before initiating a new connection
  3. Check application logic for redundant reconnect triggers on healthy connections
  4. If the socket is stuck in CONNECTING, wait for the timeout/error path before reconnecting
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at rust/perspective-js/src/ts/websocket.ts:39 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of perspective-dev/perspective@11c8238c0c (2026-09-09). Data as JSON: /api/errors/d5c3d02a9f9c043c. Report an issue: GitHub.

Appendix: source

Thrown at rust/perspective-js/src/ts/websocket.ts:39

    });

    return [sender!, receiver, reject!];
}

export async function websocket(
    WebSocket: typeof window.WebSocket,
    Client: typeof perspective_client.Client,
    url: string | URL,
    options?: { maxPayload: number },
): Promise<perspective_client.Client> {
    let client: perspective_client.Client, ws: WebSocket;

    async function connect() {
        if (
            ws?.readyState === WebSocket.CONNECTING ||
            ws?.readyState === WebSocket.OPEN
        ) {
            console.warn(`Already connected ${ws.readyState}`);
            return;
        }

        let [sender, receiver, reject] = invert_promise();

        // @ts-ignore
        ws = new WebSocket(url, options);
        ws.onopen = sender;
        ws.binaryType = "arraybuffer";
        ws.onerror = (event) => {
            // @ts-expect-error
            const msg = event.message || "Generic Websocket Error";
            client.handle_error(msg, connect);
            reject(msg);
        };

        ws.onclose = (event) => {
            // https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1

View on GitHub (pinned to 11c8238c0c)