Fix ReferenceError: WebSocket is not defined in Node.js environments #30
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The client was using global
WebSocketconstants at lines 893 and 1076, causing aReferenceError: WebSocket is not definedin Node.js environments where WebSocket is not a global object but provided by thewslibrary.This error occurred during WebSocket reconnection attempts and when checking connection status during polling fallback.
Solution
Enhanced the
WebSocketClientclass to expose WebSocket state constants through getter methods that delegate to the appropriate implementation (browser's native WebSocket or thewslibrary):Updated client.ts to use the socket's constants instead of global references:
Changes
this.socket.OPENandthis.socket.CONNECTINGinstead of globalWebSocketconstantsBenefits
✅ 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.OPENandWebSocket.CONNECTINGwere highlighted as problematic global references.Original prompt
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.