An enhanced module to provide a robust, socket-like abstraction for WebSockets
Project description
Webshocket
Webshocket is a Python library that handles the boilerplate of building WebSocket applications. It provides a high-level, object-oriented layer on top of the excellent websockets library, designed to let you focus on your application's features instead of its plumbing.
Why Use Webshocket?
Building real-time applications involves more than just sending messages. You have to manage connection lifecycles, handle state for thousands of clients, and structure your code to be maintainable. Webshocket is designed to solve these problems for you.
-
A Cleaner API: Instead of writing raw protocol handlers, you work with clean, powerful objects like WebSocketServer and ClientConnection
-
Effortless State Management: Need to remember a user's name or authentication status? Just assign it directly:
connection.username = "alice". No more managing complicated external dictionaries to track client state. -
Built-in Best Practices: async with provides safe, automatic resource management for both servers and clients, while the handler pattern promotes a clean separation between network and application logic.
Quick Start
Get a simple echo server running in seconds.
1. Server Code (server.py)
import asyncio
import webshocket
# Define your application logic by inheriting from WebSocketHandler
class EchoHandler(webshocket.WebSocketHandler):
async def on_connect(self, connection: webshocket.ClientConnection):
print(f"New connection: {connection.remote_address}")
# The smart send method automatically wraps raw strings into a Packet
await connection.send("Welcome to the Echo Server!")
async def on_receive(self, connection: webshocket.ClientConnection, packet: webshocket.Packet):
# The server automatically parses data into a Packet object
print(f"Received '{packet.data}' from {connection.remote_address}")
await connection.send(f"Echo: {packet.data}")
# Start the server
async def main():
server = webshocket.WebSocketServer("localhost", 8765, handler_class=EchoHandler)
print("Starting server...")
async with server:
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())
2. Client Code (client.py)
import asyncio
import webshocket
async def main():
uri = "ws://localhost:8765"
try:
async with webshocket.WebSocketClient(uri) as client:
print("Connected to server.")
# The recv() method automatically parses incoming messages into Packets
welcome_packet = await client.recv()
print(f"Server says: '{welcome_packet.data}'")
# Send a message and wait for the echo
await client.send("Hello from Denpasar!")
echo_packet = await client.recv()
print(f"Server echoed: '{echo_packet.data}'")
except ConnectionRefusedError:
print("Connection failed. Is the server running?")
if __name__ == "__main__":
asyncio.run(main())
Advanced Features Made Simple
Webshocket is more than just a simple wrapper. It provides a framework for building sophisticated real-time applications.
-
Channels and Broadcasting: Move beyond simple echo servers. The publish() and broadcast() methods provide a high-level API for building multi-user chat rooms and notification systems, handling the complexity of concurrent message delivery for you.
-
Secure and Resilient Connections: Easily enable secure wss:// with TLS certificates. The included WebSocketClient can be configured to automatically survive network drops with a production-ready reconnection strategy.
-
Structured and Validated Data: Enforce a strict data protocol by defining your message types with Pydantic. The library will automatically validate incoming packets, rejecting malformed or malicious data before your code ever sees it.
-
Remote Procedure Calls (RPC): Call server-side functions directly from your client! Define methods on your
WebSocketHandlerwith the@rpc_methoddecorator, and Webshocket handles the magic of remote execution, making client-server interactions feel like local function calls.
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.1.5.tar.gz.
File metadata
- Download URL: webshocket-0.1.5.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6d676f84037bcb9e527105f3f7c45a5241f3c5adcd029b45036e904bc4366de
|
|
| MD5 |
460f42028f4c1d0a36e699a9015d1a72
|
|
| BLAKE2b-256 |
0ee3c5881eafe86c5921b559b86a784ebafdfa8cd2f8c358acded37fa463b1c6
|
Provenance
The following attestation bundles were made for webshocket-0.1.5.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.1.5.tar.gz -
Subject digest:
c6d676f84037bcb9e527105f3f7c45a5241f3c5adcd029b45036e904bc4366de - Sigstore transparency entry: 253244990
- Sigstore integration time:
-
Permalink:
floydous/webshocket@579ec7e00c2bbf1d3ce75edbf4179d66daf66de0 -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/floydous
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@579ec7e00c2bbf1d3ce75edbf4179d66daf66de0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file webshocket-0.1.5-py3-none-any.whl.
File metadata
- Download URL: webshocket-0.1.5-py3-none-any.whl
- Upload date:
- Size: 19.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff4d13840c9d0351a91263e443f4fdf0df3b53d2fb2d1bb4e50e1397a7ddb235
|
|
| MD5 |
851838222b33713307b5db999ad5f71d
|
|
| BLAKE2b-256 |
1cc6df4cfc2809e36ef81977717d7426e63d697977518af1f256e3a600422258
|
Provenance
The following attestation bundles were made for webshocket-0.1.5-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.1.5-py3-none-any.whl -
Subject digest:
ff4d13840c9d0351a91263e443f4fdf0df3b53d2fb2d1bb4e50e1397a7ddb235 - Sigstore transparency entry: 253244992
- Sigstore integration time:
-
Permalink:
floydous/webshocket@579ec7e00c2bbf1d3ce75edbf4179d66daf66de0 -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/floydous
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@579ec7e00c2bbf1d3ce75edbf4179d66daf66de0 -
Trigger Event:
release
-
Statement type: