Skip to main content

An enhanced module to provide a robust, socket-like abstraction for WebSockets

Project description

docs Build Status PyPI Downloads PyPI version License Code style: ruff

[!WARNING] Webshocket is still unfinished and is not ready for proper-project use. It is advised to not expect any stability from this project until it reaches a stable release (>=v0.5.0)

Webshocket

Webshocket is a lightweight Python framework for building WebSocket RPC applications with per-client state and channel routing, built on the WebSocket library.

Usage

Calling RPC Methods

class Handler(webshocket.WebSocketHandler):
    @webshocket.rpc_method(alias_name="add")
    async def add(self, _: webshocket.ClientConnection, a: int, b: int):
        return a + b


async def main():
    server = webshocket.WebSocketServer("127.0.0.1", 5000, clientHandler=Handler)
    await server.start()

    async with webshocket.WebSocketClient("ws://127.0.0.1:5000") as client:
        response_packet = await client.send_rpc("add", 1, 2)
        print(response_packet.data)  # 3
    await server.close()


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

Managing Client State

class Handler(webshocket.WebSocketHandler):
    @webshocket.rpc_method()
    async def register(self, connection: webshocket.ClientConnection, favorite_color: str) -> None:
        connection.favorite_color = favorite_color

    @webshocket.rpc_method(alias_name="my-favorite-color")
    async def tell(self, connection: webshocket.ClientConnection) -> str:
        return connection.session_state["favorite_color"]


async def main():
    server = webshocket.WebSocketServer("127.0.0.1", 5000, clientHandler=Handler)
    await server.start()

    async with webshocket.WebSocketClient("ws://127.0.0.1:5000") as client:
        await client.send_rpc("register", "blue")

        response = await client.send_rpc("my-favorite-color")
        print(response.data)  # blue
    await server.close()


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

Working With Channels

class Handler(webshocket.WebSocketHandler):
    @webshocket.rpc_method()  # Alias name automatically set to the method name if `alias_name` is not provided
    async def subscribe_to(self, connection: webshocket.ClientConnection, channel: str):
        connection.subscribe(channel)


async def main():
    server = webshocket.WebSocketServer("127.0.0.1", 5000, clientHandler=Handler)
    await server.start()

    SportsSubscriber = await webshocket.WebSocketClient("ws://127.0.0.1:5000").connect()
    NewsSubscriber = await webshocket.WebSocketClient("ws://127.0.0.1:5000").connect()

    try:
        await SportsSubscriber.send_rpc("subscribe_to", "sports")
        await NewsSubscriber.send_rpc("subscribe_to", "news")
        await asyncio.sleep(1)

        await server.publish("sports", "Sports News!")
        await server.publish("news", "News update!")
        await asyncio.sleep(1)
        print((await SportsSubscriber.recv()).data)  # Sports News!
        print((await NewsSubscriber.recv()).data)  # News update!
    finally:
        await SportsSubscriber.close()
        await NewsSubscriber.close()

    await server.close()


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

Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request on our GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webshocket-0.4.0.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

webshocket-0.4.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file webshocket-0.4.0.tar.gz.

File metadata

  • Download URL: webshocket-0.4.0.tar.gz
  • Upload date:
  • Size: 27.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for webshocket-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ba150047411807cbb885c3d349aaa9ffbe68b7550d34d8c649868ccd039f064e
MD5 34f8869952fa11b9fe5227dcb21bde9a
BLAKE2b-256 342ce6e7f1c7b33d8ccea69d2e6951db5e431735fce750cd550e127990ec9ef6

See more details on using hashes here.

Provenance

The following attestation bundles were made for webshocket-0.4.0.tar.gz:

Publisher: publish.yml on floydous/webshocket

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webshocket-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: webshocket-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for webshocket-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 792277544f90a97ec614ce5d77d3988acf644db957cc7eb697adf5bc1dc73b60
MD5 5ed2514581254434fdc87812c991c7bd
BLAKE2b-256 d781b0ab6f103909f7ce45703a051a86a31eb051883666807c4da2b2c1722d09

See more details on using hashes here.

Provenance

The following attestation bundles were made for webshocket-0.4.0-py3-none-any.whl:

Publisher: publish.yml on floydous/webshocket

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page