-
-
Notifications
You must be signed in to change notification settings - Fork 576
[WIP] Add websockets support to Sanic #3903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[WIP] Add websockets support to Sanic #3903
Conversation
the native sanic test client appears to be useless for testing websockets. they claim sanic apps can be run as asgi apps, but that changes the whole interface for websocket objects and breaks everything. nice
Reviewer's GuideThis PR adds full WebSocket support for Sanic by introducing a custom WebSocket adapter, extending the GraphQLView to handle WebSocket routes and protocols (including initialization parameters and runtime methods), enhancing the Sanic test client to exercise subscription endpoints, and updating the base view’s type signatures to accommodate WebSocket requests. New Class Diagram: SanicWebSocketAdapterclassDiagram
class AsyncWebSocketAdapter {
%% Base class methods (conceptual)
}
class SanicWebSocketAdapter {
+ws: Websocket
+__init__(view: AsyncBaseHTTPView, request: Websocket, response: Websocket)
+iter_json() AsyncGenerator~object, None~
+send_json(message: Mapping~str, object~) None
+close(code: int, reason: str) None
}
AsyncWebSocketAdapter <|-- SanicWebSocketAdapter
Updated Class Diagram: GraphQLView and AsyncBaseHTTPView WebSocket HandlingclassDiagram
class AsyncBaseHTTPView {
+websocket_adapter_class: Callable[[Self, WebSocketRequest, WebSocketResponse], AsyncWebSocketAdapter] // Type hint 'Self' adopted
}
class GraphQLView {
+keep_alive: bool // New attribute
+keep_alive_interval: float // New attribute
+debug: bool // New attribute
+protocols: Sequence[str] // New attribute (from subscription_protocols)
+connection_init_wait_timeout: timedelta // New attribute
+websocket_adapter_class: SanicWebSocketAdapter // Concrete adapter type assigned
+get_root_value(request: Union[Request, Websocket]) Optional[RootValue] // Parameter type updated
+get_context(request: Union[Request, Websocket], response: Union[TemporalResponse, Websocket]) Context // Parameter types updated
+websocket(request: Request, ws: Websocket) Websocket // New method for WebSocket route
+is_websocket_request(request: Union[Request, Websocket]) TypeGuard[Websocket] // Parameter type and return updated
+pick_websocket_subprotocol(request: Websocket) Optional[str] // Parameter type updated
+create_websocket_response(request: Websocket, subprotocol: Optional[str]) Websocket // Parameter type and return updated
}
AsyncBaseHTTPView <|-- GraphQLView
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3903 +/- ##
==========================================
- Coverage 95.06% 94.94% -0.12%
==========================================
Files 515 515
Lines 33583 33634 +51
Branches 1736 1739 +3
==========================================
+ Hits 31924 31934 +10
- Misses 1377 1419 +42
+ Partials 282 281 -1 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #3903 will not alter performanceComparing Summary
|
Description
TODO:
WebSocket
at all, breaking everythingHTTPMethodView
cannot handle websockets. We could look at Blueprints or ask users to register route handlers individually./graphql
)Types of Changes
Issues Fixed or Closed by This PR
Summary by Sourcery
Add support for GraphQL subscriptions over WebSockets in Sanic by implementing a dedicated WebSocket adapter and extending the GraphQLView to handle WS connections, along with updated test client infrastructure.
New Features:
Enhancements:
Tests: