Skip to main content

serverwamp

Adds Web Application Messaging Protocol features to WebSocket servers. serverwamp makes it easy to both serve requests from clients and push events to clients over a single connection. Currently supports ASGI servers using asyncio or Trio and aiohttp servers.

Documentation

Usage Example

import asyncio

from aiohttp import web
import serverwamp

docs_api = serverwamp.RPCRouteSet()
docs_events = serverwamp.TopicRouteSet()

@docs_api.route('docs.getDoc')
async def get_doc(document_id):
    record = await my_db.retrieve(document_id)
    return {'status': 'SUCCESS', 'doc': record}

@docs_api.route('docs.deleteDoc')
async def delete_doc(document_id):
    succeeded = await my_db.delete(document_id)
    if succeeded:
        return {'status': 'SUCCESS'}

    return serverwamp.RPCErrorResult(
        'wamp.error.delete_failed',
        kwargs={'status': 'FAILURE'}
    )

@docs_events.route('docs.changes')
async def subscribe_to_doc_changes(session, changes_subscribers):
    changes_subscribers.add(session)
    yield
    changes_subscribers.remove(session)


async def server_context(app):

    async def watch_for_docs_changes():
        async for event in my_db.watch_changes():
            # A copy, as sessions can unsubscribe while events are sent.
            for session in list(app['changes_subscribers']):
                await session.send_event(
                    'docs.changes',
                    args=(f'Document changed: {event}',)
                )

    watcher = asyncio.create_task(watch_for_docs_changes())
    try:
        yield
    finally:
        watcher.cancel()


if __name__ == '__main__':
    changes_subscribers = set()

    app = serverwamp.Application()
    app.add_rpc_routes(docs_api)
    app.add_topic_routes(docs_events)
    app.set_default_arg('changes_subscribers', changes_subscribers)

    http_app = web.Application()
    http_app['changes_subscribers'] = changes_subscribers
    http_app.add_routes((
        web.get('/', app.aiohttp_websocket_handler()),
    ))
    http_app.cleanup_ctx.append(server_context)

    web.run_app(http_app)

Development

Unit tests can be run with:

pip install -e . --group ci
pytest

Linting and type checking can be run with:

ruff check serverwamp tests examples
mypy serverwamp

Metadata

Release files for serverwamp 2.0.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 serverwamp 2.0.0
File Size Uploaded Provenance Actions
serverwamp-2.0.0.tar.gz 27.5 kB Without provenance Details for serverwamp-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for serverwamp 2.0.0
File Interpreter ABI Platform Actions
serverwamp-2.0.0-py3-none-any.whl Python 3 none any Details for serverwamp-2.0.0-py3-none-any.whl

Total release size: 58.0 kB

Release files / serverwamp-2.0.0.tar.gz

Download URL serverwamp-2.0.0.tar.gz
Size 27.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cf81debd63f4c326985e4ce9d2a338ea0065fa3f57443a31a642ad52d9f7ee26
BLAKE2b-256 checksum
How to use checksums
1600ec33b573f1b875cddacec3f59b96f6236131ca659d3b38c04008d8772b6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release files / serverwamp-2.0.0-py3-none-any.whl

Download URL serverwamp-2.0.0-py3-none-any.whl
Size 30.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
223643e3ef3d60fd0e56e568c3be6fd70af7f6586e173772c2b802bb87cb7cb0
BLAKE2b-256 checksum
How to use checksums
e6f6ece09ce27772ba7a807d9f864d0c732c55b1b0fa0a9e49012d16dc463336
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Security for serverwamp 2.0.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