Private
Public Access
Invite-only FastAPI + SQLAlchemy(async) + Postgres backend (session-cookie auth via CLI-provisioned accounts, open-room CRUD, single-instance /ws/chat) and a React + Vite PWA frontend (login, room list, chat view). Backend tests pass against a local Postgres DB. See README.md and backend/README.md for setup, and ARCHITECTURE.md for the full phased design. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
import uuid
|
|
from collections import defaultdict
|
|
|
|
from fastapi import WebSocket
|
|
|
|
|
|
class ConnectionManager:
|
|
"""In-memory, single-process WebSocket registry.
|
|
|
|
Correct for a single app-server instance only; cross-instance fan-out via
|
|
Redis pub/sub is a later phase (ARCHITECTURE.md phase 5).
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
self._rooms: dict[uuid.UUID, set[WebSocket]] = defaultdict(set)
|
|
|
|
def join(self, room_id: uuid.UUID, websocket: WebSocket) -> None:
|
|
self._rooms[room_id].add(websocket)
|
|
|
|
def leave(self, room_id: uuid.UUID, websocket: WebSocket) -> None:
|
|
self._rooms[room_id].discard(websocket)
|
|
if not self._rooms[room_id]:
|
|
del self._rooms[room_id]
|
|
|
|
def leave_all(self, websocket: WebSocket) -> None:
|
|
for room_id in list(self._rooms.keys()):
|
|
self.leave(room_id, websocket)
|
|
|
|
async def broadcast(self, room_id: uuid.UUID, payload: dict) -> None:
|
|
for websocket in list(self._rooms.get(room_id, ())):
|
|
await websocket.send_json(payload)
|