Add WebSocket request support as alternative to HTTP API calls #204
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.
Overview
This PR implements the ability to route API requests through WebSocket connections instead of HTTP when the WebSocket feature flag is enabled and a connection is active. This provides a more efficient real-time communication channel while maintaining full backward compatibility.
Problem Statement
The application previously used only HTTP REST API calls for all operations. With WebSocket infrastructure already in place, we needed a way to route API requests through WebSocket connections when available to improve performance and reduce overhead.
Solution
Added intelligent request routing that checks if WebSocket is enabled and connected, then automatically routes supported API operations through WebSocket while falling back to HTTP for unsupported operations or when WebSocket is unavailable.
Implementation Details
1. Extended WebSocket Type Definitions
Added comprehensive WebSocket action types and payload definitions for all API operations:
2. Enhanced WebSocketManager
Added
sendRequest()method that implements request/response correlation:Features:
crypto.randomUUID()with fallback3. Modified Request Function
The main
Request()function now:4. URL to WebSocket Action Mapping
Comprehensive mapping function translates HTTP endpoints to WebSocket actions:
Supported Operations
All major API operations now support WebSocket routing:
Behavior
When WebSocket is enabled and connected:
When WebSocket is disabled or disconnected:
Key Features
✅ Feature flag controlled (
useWebsockets)✅ Connection status validation
✅ Automatic HTTP fallback on errors
✅ Request/response correlation via UUID
✅ Response structure validation
✅ Type-safe error handling
✅ Zero breaking changes
✅ Full backward compatibility
Testing
Code Quality Improvements
HTTP_STATUS_NO_CONTENTconstant to module levelanytypes with properFailureResponseinterfaceExample Usage
No code changes required! Simply enable the WebSocket feature flag:
All API calls will automatically route through WebSocket when connected, with transparent fallback to HTTP when needed.
Original prompt
💡 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.