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

Building distributions from this repository requires Git, Node.js >=22 and npm: the browser client is built from current wsrpc-ts sources. Installing a wheel or building from an sdist does not require Node.js. See client build details for revision selection and provenance.

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.2.1

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.2.1
File Size Uploaded Provenance Actions
wsrpc_asgi-0.2.1.tar.gz 55.1 kB With provenance Details for wsrpc_asgi-0.2.1.tar.gz

Built distribution (wheel)

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

Total release size: 106.3 kB

Release files / wsrpc_asgi-0.2.1.tar.gz

Download URL wsrpc_asgi-0.2.1.tar.gz
Size 55.1 kB
Tags Source
SHA-256 checksum
How to use checksums
863afd173f76a5f399b1e2fb148fba3afa464cd897f10c9314e6bc9d6be91ce1
BLAKE2b-256 checksum
How to use checksums
1cd801a4bf3eb94aa5b19c8d35f74c933eb8c96b043c34d6e0b2894b2f32300d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

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

Download URL wsrpc_asgi-0.2.1-py3-none-any.whl
Size 51.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e52ae9dd01a7fc2641026c87fd482e513accf199b6b4c96a6d05a21ffc368ce8
BLAKE2b-256 checksum
How to use checksums
3783242c5b5deb8e2bcf67c149d6166b82256cc35ef6b6080844244e45be0529
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Security for wsrpc-asgi 0.2.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for wsrpc_asgi-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

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