| name | cali-coding-starstream |
| description | Adds real-time broadcasting, multi-user collaboration, and database persistence to StarHTML applications. Use when implementing chat systems, collaborative editors, presence tracking, typing indicators, or any feature requiring synchronized state across clients. Supports core StarStream (broadcasting), PocketBase (persistence), and Loro (CRDT) plugins. |
StarStream: Real-time Collaboration for StarHTML
StarStream provides a "convention over configuration" approach to real-time features in StarHTML. Follow these instructions to implement synchronized user experiences.
Core Integration Strategy
When tasked with adding real-time features, always follow this workflow:
- Identify Features: Determine if the app needs simple broadcasting, persistence (PocketBase), or conflict-free editing (Loro).
- Initialize Core: Add
StarStreamPlugin(app) to the main application file.
- Enable Modules: Toggle
enable_presence, enable_typing, enable_cursor, or enable_history as needed.
- Register SSE: Ensure a
/stream route exists to provide the EventSource connection.
- Emit Events: Use
yield elements(...) in @sse routes to auto-broadcast UI updates.
Basic Initialization
from starhtml import *
from starstream import StarStreamPlugin
app, rt = star_app()
stream = StarStreamPlugin(app, enable_presence=True, enable_typing=True)
@rt("/chat/{room_id}")
@sse
async def chat(room_id: str, msg: str):
yield elements(Div(msg), "#messages", "append")
Plugin Selection Guide
- StarStreamCore: Simple broadcasting, ephemeral state.
- PocketBase: Persistent data with real-time sync.
- Loro: Multi-user document editing (CRDT).
Implementation Checklist
Validation & Testing
Always validate the real-time implementation using the provided test suite or by running an example:
pytest tests/test_core.py tests/test_presence.py
python examples/full_features.py
Broadcasting
Fire-and-forget broadcast to all connected clients:
@rt("/todos/add", methods=["POST"])
@sse
def add_todo(text: str):
todos.append(text)
stream.broadcast(
elements(render_todos(), "#todo-list"),
target="todos"
)
yield elements(render_todos(), "#todo-list")
Targets
stream.broadcast(msg, target="chat")
stream.broadcast(msg, target="room:123")
stream.broadcast(msg, target="user:456")
stream.broadcast(msg)
Observability
stats = stream.get_metrics("todos")
stream.set_error_hook(lambda topic, msg, err: logger.error(f"{topic}: {err}"))
Resources
- See
REFERENCE.md for detailed plugin API and advanced usage.
- Full Demo:
examples/full_features.py shows all features integrated.