PyStrands
A lightweight, framework-agnostic real-time communication library for Python applications.
What is PyStrands?
PyStrands provides a client-server architecture for building real-time features without the complexity. A Go-based WebSocket broker handles client connections, while Python backends process messages via TCP β enabling horizontal scaling and clean separation of concerns.
β¨ Features
- Sync & Async clients β Choose between
PyStrandsClient(threading) andAsyncPyStrandsClient(asyncio) - Room-based messaging β Organize clients into rooms for targeted communication
- Private & broadcast messaging β Send to specific clients or broadcast to everyone
- Connection request handling β Authenticate and route clients on connect
- Auto-reconnect β Built-in exponential backoff for resilient connections
- Message queuing β Go broker buffers messages when backends are down
- Framework-agnostic β Works with Flask, FastAPI, Django, or standalone
- Production tested β 66 tests including real-world production scenarios
π Quick Install
π§ Quick Start
import asyncio
from pystrands import AsyncPyStrandsClient
class ChatBackend(AsyncPyStrandsClient):
async def on_connection_request(self, request):
request.context.room_id = request.url.strip("/")
return True
async def on_message(self, message, context):
await self.send_room_message(context.room_id, f"echo: {message}")
client = ChatBackend(host="localhost", port=8081)
asyncio.run(client.run_forever())
from pystrands import PyStrandsClient
class ChatBackend(PyStrandsClient):
def on_connection_request(self, request):
request.context.room_id = request.url.strip("/")
return True
def on_message(self, message, context):
self.send_room_message(context.room_id, f"echo: {message}")
client = ChatBackend(host="localhost", port=8081)
client.run_forever()
ποΈ Architecture Overview
ββββββββββββ WebSocket ββββββββββββ TCP ββββββββββββββββ
β Browser β ββββββββββββββββ β Go Broker β ββββββββββββββ β Python Backendβ
β Client β β β β (your code) β
ββββββββββββ β Queue: β ββββββββββββββββ
β [msg1] β ββββββββββββββββ
β [msg2] β ββββββββββββββ β Python Backendβ
β [msg3] β β (replica 2) β
ββββββββββββ ββββββββββββββββ
- :material-rocket-launch: **Get Started**
---
Install PyStrands and build your first real-time backend in minutes.
[β Getting Started](getting-started.md)
- :material-book-open: **Guides**
---
Learn about architecture, authentication, and messaging patterns.
[β Architecture](guide/architecture.md)
- :material-code-braces: **API Reference**
---
Complete API documentation for clients and context classes.
[β API Reference](api/client.md)
Requirements
- Python >= 3.10
License
MIT β see GitHub repository for details.