Skip to content

Conversation

Copy link

Copilot AI commented Oct 24, 2025

Problem

The client was using global WebSocket constants at lines 893 and 1076, causing a ReferenceError: WebSocket is not defined in Node.js environments where WebSocket is not a global object but provided by the ws library.

// ❌ Before - fails in Node.js
if (this.socket.client.readyState !== WebSocket.OPEN && 
    this.socket.client.readyState !== WebSocket.CONNECTING) {
  // ...
}

This error occurred during WebSocket reconnection attempts and when checking connection status during polling fallback.

Solution

Enhanced the WebSocketClient class to expose WebSocket state constants through getter methods that delegate to the appropriate implementation (browser's native WebSocket or the ws library):

// src/socket.ts
export class WebSocketClient {
  // Expose WebSocket state constants
  get CONNECTING() { return this.webSocketImpl.CONNECTING; }
  get OPEN() { return this.webSocketImpl.OPEN; }
  get CLOSING() { return this.webSocketImpl.CLOSING; }
  get CLOSED() { return this.webSocketImpl.CLOSED; }
  // ...
}

Updated client.ts to use the socket's constants instead of global references:

// ✅ After - works in both browser and Node.js
if (this.socket.client.readyState !== this.socket.OPEN && 
    this.socket.client.readyState !== this.socket.CONNECTING) {
  // ...
}

Changes

  • src/socket.ts: Added 4 getter methods to expose WebSocket state constants from the implementation
  • src/client.ts: Updated 2 locations (lines 893 and 1076) to use this.socket.OPEN and this.socket.CONNECTING instead of global WebSocket constants

Benefits

✅ Works in both browser and Node.js environments
✅ Supports custom WebSocket implementations
✅ No breaking changes to the API
✅ Type-safe solution
✅ Minimal code changes

Fixes the issue shown in the attached screenshot where WebSocket.OPEN and WebSocket.CONNECTING were highlighted as problematic global references.

Original prompt

This section details on the original issue you should resolve

<issue_title>ReferenceError: WebSocket is not defined</issue_title>
<issue_description>Image

It seems that the client here still uses the global websocket</issue_description>

<agent_instructions>client should not use the global web socket, recheck it at line 890 -> 900 of client, ensure it working correctly</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #29


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 24, 2025 09:58
Co-authored-by: tctien342 <26409306+tctien342@users.noreply.github.com>
…bSocketClient

Co-authored-by: tctien342 <26409306+tctien342@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ReferenceError: WebSocket is not defined Fix ReferenceError: WebSocket is not defined in Node.js environments Oct 24, 2025
Copilot finished work on behalf of tctien342 October 24, 2025 10:04
Copilot AI requested a review from tctien342 October 24, 2025 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReferenceError: WebSocket is not defined

2 participants