Repository navigation
feat(websocket): native RSGI WebSocket support - #91
Merged
Merged
Conversation
Adds first-class WebSocket support over Granian RSGI without going through the (now-removed) ASGI bridge: - `AppState` gains a dedicated `websocket: Mutex<Router<usize>>` plus a `websocket_routes: Vec<WebsocketRoute>` table, and `CompiledRouters` snapshots it together with the HTTP routers so WS lookup is also lock-free after `freeze()`. - `App.add_websocket_route(path, handler)` registers a coroutine handler in the Rust state and clears the cached compiled snapshot so the next request rebuilds it. - `run_rsgi` now branches on `scope.proto == "websocket"` to a new `run_rsgi_websocket` dispatcher: matches the WS router, builds a `WebSocket` helper, invokes the handler (sync or async) and closes with 1011 if the handler raises before/after await. - New `WebSocket` pyclass (exported as `oxyroute._oxyroute.WebSocket`) wraps the Granian `RSGIWebsocketProtocol` / transport directly. All async methods delegate to the underlying coroutine via `pyo3_async_runtimes::tokio::into_future` to keep the Tokio bridge cost off the hot path of HTTP dispatch. The Python `@app.websocket(path)` decorator wiring lands in the follow-up commit.
WebSocket.close() previously returned plain Python None, which Python
refuses to await ('object NoneType can't be used in await expression').
Handlers naturally write 'await ws.close()', so promote the method to
return an immediately-resolving awaitable backed by future_into_py.
Also avoid calling protocol.close() a second time after the peer has
already disconnected (kind=0) — receive_text/receive_bytes mark the
WebSocket as closed before raising RuntimeError, so the user's followup
'await ws.close()' becomes a no-op.
- Adds App.websocket(path) registration decorator that delegates to the native add_websocket_route binding. Path syntax matches HTTP routes (matchit '/ws/:room'); the handler receives a single oxyroute.WebSocket argument. - Re-exports WebSocket from the package so users can write 'from oxyroute import App, WebSocket'.
Drives App.handle_rsgi directly with mocked Granian-style WebSocket scope/protocol/transport objects (no ASGI shim). Covers: - echo round-trip with path params (text + bytes) - send_json convenience helper - no matching route → polite close 1000 - handler raising → close 1011 - peer-side close (kind=0) → ws.is_closed True, no double close - text/binary frame kind mismatch → ValueError - send before accept → RuntimeError - @app.websocket() rejected after freeze()
- New docs/websocket.md walks through @app.websocket(path), the oxyroute.WebSocket API surface (accept / receive* / send* / close), routing semantics, error handling and the in-process testing pattern. - Linked from docs/index.md table of contents. - CHANGELOG bumped: Added section now covers the native WebSocket support that supersedes the removed ASGI spike.
ZhuchkaTriplesix
added a commit
that referenced
this pull request
Jul 20, 2026
feat(websocket): native RSGI WebSocket support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #90.
Summary
OxyRoute v0.3.0 dropped the ASGI bridge alongside the legacy ASGI WebSocket
spike. This PR re-introduces WebSocket support natively over RSGI, with no
Python helper class and no second event loop.
WebSocketpyclass (oxyroute._oxyroute.WebSocket) wraps Granian'sRSGIWebsocketProtocol/ transport.accept,receive/receive_text/receive_bytes,send_text/send_bytes/send_jsonand
close(code=None)all return real awaitables that delegate to theunderlying Granian coroutines via
pyo3_async_runtimes::tokio::into_future.App.add_websocket_route(path, handler)registers in a dedicatedmatchit::Routernext to the HTTP routers (also covered byfreeze()'scompiled snapshot for lock-free lookup).
run_rsgibranches onscope.proto == "websocket"to a newrun_rsgi_websocketdispatcher. Unknown paths → politeclose(1000);handler errors →
close(1011).@app.websocket(path)decorator +oxyroute.WebSocketre-exported from the package.
Test plan
tests/test_websocket_native.py— 9 cases covering: echo round-tripwith path params (text + bytes),
send_json, no-route close, handlerraise → 1011, peer-side close (kind=0) → no double close, frame kind
mismatch, send-before-accept, frozen app rejects new ws routes.
pytest(102 / 102) green.cargo fmt/cargo clippy --all-targets -- -D warningsclean.ruff check/ruff format --checkclean on Python sources.Out of scope / follow-ups
the existing pattern in
tests/test_granian_e2e.py).intentionally does not expose a subprotocol parameter on
accept).