An enhanced module to provide a robust, socket-like abstraction for WebSockets
Project description
[!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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba150047411807cbb885c3d349aaa9ffbe68b7550d34d8c649868ccd039f064e
|
|
| MD5 |
34f8869952fa11b9fe5227dcb21bde9a
|
|
| BLAKE2b-256 |
342ce6e7f1c7b33d8ccea69d2e6951db5e431735fce750cd550e127990ec9ef6
|
Provenance
The following attestation bundles were made for webshocket-0.4.0.tar.gz:
Publisher:
publish.yml on floydous/webshocket
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
webshocket-0.4.0.tar.gz -
Subject digest:
ba150047411807cbb885c3d349aaa9ffbe68b7550d34d8c649868ccd039f064e - Sigstore transparency entry: 836076142
- Sigstore integration time:
-
Permalink:
floydous/webshocket@94a551b84c9af8f97fa5bddb1dff6a47429cfbab -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/floydous
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@94a551b84c9af8f97fa5bddb1dff6a47429cfbab -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
792277544f90a97ec614ce5d77d3988acf644db957cc7eb697adf5bc1dc73b60
|
|
| MD5 |
5ed2514581254434fdc87812c991c7bd
|
|
| BLAKE2b-256 |
d781b0ab6f103909f7ce45703a051a86a31eb051883666807c4da2b2c1722d09
|
Provenance
The following attestation bundles were made for webshocket-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on floydous/webshocket
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
webshocket-0.4.0-py3-none-any.whl -
Subject digest:
792277544f90a97ec614ce5d77d3988acf644db957cc7eb697adf5bc1dc73b60 - Sigstore transparency entry: 836076143
- Sigstore integration time:
-
Permalink:
floydous/webshocket@94a551b84c9af8f97fa5bddb1dff6a47429cfbab -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/floydous
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@94a551b84c9af8f97fa5bddb1dff6a47429cfbab -
Trigger Event:
release
-
Statement type: