WebSocket Server
The WebSocket server provides real-time push notifications to connected browser tabs so the web frontend can stay in sync when entries are created, updated, or deleted -- either by the same user in another tab or by an external process (CLI, MCP).
Key Files
| File | Purpose | |------|---------| | `pyrite/server/websocket.py` | `ConnectionManager` class and module-level `manager` singleton | | `pyrite/server/endpoints/entries.py` | Broadcasts events after entry mutations | | `pyrite/server/endpoints/admin.py` | Broadcasts `kb_synced` after index sync |
`ConnectionManager` Class
Methods
Properties
Singleton
The module exports a `manager` instance (`ConnectionManager()`) imported directly by endpoint modules.
Event Types
All events are JSON objects with a `type` field, `entry_id`, and `kb_name`:
| Event | Trigger | |-------|---------| | `entry_created` | After `KBService.create_entry()` succeeds in the entries endpoint | | `entry_updated` | After `KBService.update_entry()` succeeds in the entries endpoint | | `entry_deleted` | After `KBService.delete_entry()` succeeds in the entries endpoint | | `kb_synced` | After `KBService.sync_index()` completes in the admin endpoint |
Broadcast Pattern
``` Client A (browser tab) ──ws──> ConnectionManager Client B (browser tab) ──ws──> | | REST endpoint mutates entry ──> broadcast({type, entry_id, kb_name}) | sends JSON to A and B ```
Events are fire-and-forget: the broadcast is awaited but failures on individual connections are silently handled by removing the dead socket.