{"record":{"id":"d5c3d02a9f9c043c","repo":"perspective-dev/perspective","slug":"already-connected-ws-readystate","errorCode":null,"errorMessage":"Already connected ${ws.readyState}","messagePattern":"Already connected (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"rust/perspective-js/src/ts/websocket.ts","lineNumber":39,"sourceCode":"    });\n\n    return [sender!, receiver, reject!];\n}\n\nexport async function websocket(\n    WebSocket: typeof window.WebSocket,\n    Client: typeof perspective_client.Client,\n    url: string | URL,\n    options?: { maxPayload: number },\n): Promise<perspective_client.Client> {\n    let client: perspective_client.Client, ws: WebSocket;\n\n    async function connect() {\n        if (\n            ws?.readyState === WebSocket.CONNECTING ||\n            ws?.readyState === WebSocket.OPEN\n        ) {\n            console.warn(`Already connected ${ws.readyState}`);\n            return;\n        }\n\n        let [sender, receiver, reject] = invert_promise();\n\n        // @ts-ignore\n        ws = new WebSocket(url, options);\n        ws.onopen = sender;\n        ws.binaryType = \"arraybuffer\";\n        ws.onerror = (event) => {\n            // @ts-expect-error\n            const msg = event.message || \"Generic Websocket Error\";\n            client.handle_error(msg, connect);\n            reject(msg);\n        };\n\n        ws.onclose = (event) => {\n            // https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/perspective-dev/perspective/blob/11c8238c0c9c0b9e490b5a6a2dc277afad1e980a/rust/perspective-js/src/ts/websocket.ts#L21-L57","documentation":"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.","triggerScenarios":"Thrown at rust/perspective-js/src/ts/websocket.ts:39 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Avoid calling connect while an existing socket is OPEN or CONNECTING; await its completion first","Close the current websocket explicitly before initiating a new connection","Check application logic for redundant reconnect triggers on healthy connections","If the socket is stuck in CONNECTING, wait for the timeout/error path before reconnecting"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"11c8238c0c9c0b9e490b5a6a2dc277afad1e980a","analyzedAt":"2026-09-09T00:35:19.377Z","contentChangedAt":"2026-09-09T00:35:19.377Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}