A lightweight, framework-agnostic real-time library for Python applications.
Project description
PyStrands
A lightweight, framework-agnostic real-time library for Python applications.
Overview
PyStrands is a simple and efficient real-time communication library that enables developers to build real-time features into their Python applications. It provides a client-server architecture with support for room-based messaging, private messaging, and broadcast capabilities.
Features
- Real-time bidirectional communication
- Room-based messaging
- Private messaging between clients
- Broadcast messaging
- Connection request handling
- Metadata support for clients
- Framework-agnostic design
- Simple and intuitive API
- Easy-to-use CLI for server management
Installation
pip install pystrands
Quick Start
Running the Server
To start the PyStrands server, use the CLI command:
python -m pystrands server [options]
The server binary will be automatically downloaded for your platform and architecture. All arguments after server will be passed to the server binary.
Example:
python -m pystrands server --ws-port 8080 --tcp-port 8081
Basic Client Implementation
from pystrands import PyStrandsClient
class MyClient(PyStrandsClient):
def on_connection_request(self, request):
print(f"Connection request from {request.context.client_id} in room {request.context.room_id}")
# change room id or client id
request.context.room_id = "new_room" # by default it is websocket url endpoint
request.context.client_id = "new_client_id" # by default it is uuid
request.context.metadata = {"name": "John Doe"} # additional metadata
# accept the connection
request.accepted = True # accepted by default
def on_connect(self, context):
print(f"Connected to {context.client_id} in room {context.room_id}")
# send welcome message to the client
self.send_private_message(context.client_id, f"Welcome to the room! {context.metadata.get('name')}")
def on_message(self, message, context):
print(f"Received message: {message} from {context.context.client_id} in room {context.context.room_id}")
def on_disconnect(self, context):
print(f"Disconnected from {context.context.client_id} in room {context.context.room_id}")
def on_error(self, error, context):
print(f"Error: {error} from {context.context.client_id} in room {context.context.room_id}")
# Create and connect client
client = MyClient(host="localhost", port=8081)
client.connect()
# Send messages
client.broadcast_message("Hello everyone!")
client.send_room_message("room1", "Hello room!")
client.send_private_message("client123", "Private message!")
# Run the client
client.run_forever()
API Reference
PyStrandsClient
The main client class that handles communication with the PyStrands server.
Methods
__init__(host="localhost", port=8081): Initialize the clientconnect(): Establish connection to the serverdisconnect(): Close the connectionrun_forever(): Start the client's event loopbroadcast_message(message: str): Send a message to all connected clientssend_room_message(room_id: str, message: str): Send a message to a specific roomsend_private_message(client_id: str, message: str): Send a private message to a specific client
Callbacks
on_connect(context: Context): Called when the client successfully connectson_connection_request(context: ConnectionRequestContext): Called when a new connection request is receivedon_message(message, context: Context): Called when a message is receivedon_disconnect(context: Context): Called when the client disconnectson_error(error): Called when an error occurs
Context Classes
Context
client_id: Unique identifier for the clientroom_id: ID of the room the client is inmetadata: Additional information about the context
ConnectionRequestContext
headers: Headers of the connection requesturl: URL endpoint of the connection requestremote_addr: Remote address of the connection requestcontext: Context of the connection requestaccepted: Whether the connection request should be accepted
Requirements
- Python >= 3.8
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
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 pystrands-0.2.0.tar.gz.
File metadata
- Download URL: pystrands-0.2.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c359a3a4cef707b5be9b5dd08e7e81a6bc7454b257bc1077d2861ac51db5e713
|
|
| MD5 |
7652d43a50fceaaf1b68b0483496e07b
|
|
| BLAKE2b-256 |
a486fdd33dd7978878e010e87e9d47ebc0a74632eaa534b6879b45792de71cf2
|
Provenance
The following attestation bundles were made for pystrands-0.2.0.tar.gz:
Publisher:
publish.yml on pystrands/pystrands
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystrands-0.2.0.tar.gz -
Subject digest:
c359a3a4cef707b5be9b5dd08e7e81a6bc7454b257bc1077d2861ac51db5e713 - Sigstore transparency entry: 942438678
- Sigstore integration time:
-
Permalink:
pystrands/pystrands@63c88f7973bd0a730c9ab79f983bdb749f3657b4 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pystrands
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@63c88f7973bd0a730c9ab79f983bdb749f3657b4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pystrands-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pystrands-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.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 |
39fa0f4ea248319c1a2737f4f6bce9986f3eb899ed93d074b27fbdee0b9260f6
|
|
| MD5 |
dadb9817d994a1580b0241bd9f356566
|
|
| BLAKE2b-256 |
dba8e33d291e315cf3932a9174e05c6fadc1947251c335743cb653a645b1a044
|
Provenance
The following attestation bundles were made for pystrands-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on pystrands/pystrands
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystrands-0.2.0-py3-none-any.whl -
Subject digest:
39fa0f4ea248319c1a2737f4f6bce9986f3eb899ed93d074b27fbdee0b9260f6 - Sigstore transparency entry: 942438692
- Sigstore integration time:
-
Permalink:
pystrands/pystrands@63c88f7973bd0a730c9ab79f983bdb749f3657b4 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pystrands
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@63c88f7973bd0a730c9ab79f983bdb749f3657b4 -
Trigger Event:
release
-
Statement type: