Skip to content

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.

WebSocket Clients ←→ Go Broker ←TCPβ†’ Python Backends

✨ Features

  • Sync & Async clients β€” Choose between PyStrandsClient (threading) and AsyncPyStrandsClient (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

pip install pystrands

πŸ”§ 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.