Skip to main content

wsrpc-asgi

Bidirectional WebSocket RPC for asyncio ASGI applications. A fork of wsrpc-aiohttp with an ASGI server transport and an optional websockets Python client. Existing WSRPC Python and JavaScript peers use the same wire protocol.

Supports functions, per-connection route objects, reverse calls, events, broadcast, lifecycle signals, custom JSON codecs and threaded handlers. The base package has no runtime dependencies. Python 3.10+ is supported.

Install

pip install 'wsrpc-asgi[fastapi,client]' uvicorn

For a local checkout use uv sync and uv run for the commands below.

FastAPI server

Save as app.py:

from fastapi import FastAPI, WebSocket
from wsrpc_asgi import WebSocketAsync


class RPC(WebSocketAsync):
    pass


async def add(socket, *, a: int, b: int) -> int:
    return a + b


RPC.add_route("add", add)
app = FastAPI()


@app.websocket("/ws/")
async def rpc(websocket: WebSocket):
    await RPC.serve(websocket)

Run uvicorn app:app, then connect from Python:

import asyncio
from wsrpc_asgi import WSRPCClient


async def main():
    async with WSRPCClient("ws://127.0.0.1:8000/ws/") as client:
        print(await client.proxy.add(a=20, b=22))


if __name__ == "__main__":
    asyncio.run(main())

Verify a round trip

This self-contained example runs the same RPC endpoint through FastAPI's in-process test client:

from fastapi import FastAPI, WebSocket
from fastapi.testclient import TestClient
from wsrpc_asgi import WebSocketAsync


class RPC(WebSocketAsync):
    pass


RPC.add_route("add", lambda socket, a, b: a + b)
app = FastAPI()


@app.websocket("/ws/")
async def endpoint(websocket: WebSocket):
    await RPC.serve(websocket)


with TestClient(app) as http:
    with http.websocket_connect("/ws/") as socket:
        socket.send_json(
            {"id": 1, "method": "add", "params": {"a": 20, "b": 22}}
        )
        assert socket.receive_json() == {"id": 1, "result": 42}

Other ASGI frameworks

Use RPC.as_asgi() as a standalone ASGI application or framework route. RPCMiddleware(http_app, rpc, path="/ws/") adds a WebSocket endpoint to an existing ASGI application while forwarding its other traffic.

The framework guide includes runnable examples for FastAPI, Starlette, Django, Channels, Quart, Litestar, Falcon, Sanic, BlackSheep and bare ASGI, plus Uvicorn and Hypercorn commands. Each framework example is exercised over real HTTP and WebSocket connections.

The quick start includes a browser demo. wsrpc_asgi.STATIC_DIR contains the unchanged JavaScript clients and TypeScript declarations; they can also be obtained from @wsrpc/client.

Documentation and its runnable examples are maintained in wsrpc/docs.

Development

uv sync --locked
uv run --locked pytask
uv run --locked --group integrations pytest tests/test_examples.py

See development for the verification matrix, free-threaded Python and release workflow. Framework integration dependencies are test-only; applications install the framework they use.

Licensed under Apache-2.0. The bundled JavaScript client retains its MIT license. Upstream attribution and license notices are preserved.

Metadata

Release files for wsrpc-asgi 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wsrpc-asgi 0.1.0
File Size Uploaded Provenance Actions
wsrpc_asgi-0.1.0.tar.gz 40.3 kB Without provenance Details for wsrpc_asgi-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wsrpc-asgi 0.1.0
File Interpreter ABI Platform Actions
wsrpc_asgi-0.1.0-py3-none-any.whl Python 3 none any Details for wsrpc_asgi-0.1.0-py3-none-any.whl

Total release size: 80.0 kB

Release files / wsrpc_asgi-0.1.0.tar.gz

Download URL wsrpc_asgi-0.1.0.tar.gz
Size 40.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fba257b57561a7c27d75c8fffd39fa636c36caf3e07af124ad925167175c745c
BLAKE2b-256 checksum
How to use checksums
18baec44ff15b18d3877b7b58e4bdf55523f8f3388dfe6c2c41599cfde17c6d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / wsrpc_asgi-0.1.0-py3-none-any.whl

Download URL wsrpc_asgi-0.1.0-py3-none-any.whl
Size 39.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
af0ecf5b102c871ffc62fc9b22d4f305bb58ebc3b4d9976adc8c2bd392a4b54b
BLAKE2b-256 checksum
How to use checksums
25acf8f589696607322fbafe8b016bfc1fe8313b63160684704a1f34d3a8f703
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for wsrpc-asgi 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page