Skip to main content

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 client
  • connect(): Establish connection to the server
  • disconnect(): Close the connection
  • run_forever(): Start the client's event loop
  • broadcast_message(message: str): Send a message to all connected clients
  • send_room_message(room_id: str, message: str): Send a message to a specific room
  • send_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 connects
  • on_connection_request(context: ConnectionRequestContext): Called when a new connection request is received
  • on_message(message, context: Context): Called when a message is received
  • on_disconnect(context: Context): Called when the client disconnects
  • on_error(error): Called when an error occurs

Context Classes

Context

  • client_id: Unique identifier for the client
  • room_id: ID of the room the client is in
  • metadata: Additional information about the context

ConnectionRequestContext

  • headers: Headers of the connection request
  • url: URL endpoint of the connection request
  • remote_addr: Remote address of the connection request
  • context: Context of the connection request
  • accepted: 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

pystrands-0.2.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

pystrands-0.2.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

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

Hashes for pystrands-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c359a3a4cef707b5be9b5dd08e7e81a6bc7454b257bc1077d2861ac51db5e713
MD5 7652d43a50fceaaf1b68b0483496e07b
BLAKE2b-256 a486fdd33dd7978878e010e87e9d47ebc0a74632eaa534b6879b45792de71cf2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pystrands-0.2.0.tar.gz:

Publisher: publish.yml on pystrands/pystrands

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

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

Hashes for pystrands-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39fa0f4ea248319c1a2737f4f6bce9986f3eb899ed93d074b27fbdee0b9260f6
MD5 dadb9817d994a1580b0241bd9f356566
BLAKE2b-256 dba8e33d291e315cf3932a9174e05c6fadc1947251c335743cb653a645b1a044

See more details on using hashes here.

Provenance

The following attestation bundles were made for pystrands-0.2.0-py3-none-any.whl:

Publisher: publish.yml on pystrands/pystrands

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