forked from syntaxbullet/AuroraBot-discord
1.9 KiB
1.9 KiB
2026-01-07-websocket-realtime-data
Status: Done Created: 2026-01-07 Tags: feature, websocket, realtime, research
1. Context & User Story
- As a: Developer
- I want to: implement a WebSocket connection between the frontend and the Aurora server
- So that: I can stream real-time data (profiling, logs, events) to the dashboard without manual page refreshes.
2. Technical Requirements
Data Model Changes
- N/A
API / Interface
- Endpoint:
/ws(Upgrade Upgrade: websocket). - Protocol: Define a simple JSON message format (e.g.,
{ type: "UPDATE", data: { ... } }).
3. Constraints & Validations (CRITICAL)
- Bun Support: Use Bun's native
Bun.serve({ websocket: { ... } })capabilities if possible. - Security: Ensure that the WebSocket endpoint is not publicly abusable (consider simple token or origin check if necessary, though internal usage is primary context for now).
- Performance: Do not flood the client. Throttle updates if necessary.
4. Acceptance Criteria
- Server accepts WebSocket connections on
/ws. - Client (
script.js) successfully connects to the WebSocket. - Server sends a "Hello" or "Ping" packet.
- Client receives and logs the packet.
- (Stretch) Stream basic uptime or heartbeat every 5 seconds.
5. Implementation Plan
- Modify
src/web/server.tsto handlewebsocketupgrade inBun.serve. - Create a message handler object/function to manage connected clients.
- Update
src/web/public/script.jsto initializeWebSocket. - Test connection stability.
Implementation Notes
- Enabled
websocketinBun.servewithinsrc/web/server.ts. - Implemented a heartbeat mechanism broadcasting
HEARTBEATevents every 5s. - Updated
script.jsto auto-connect, handle reconnects, and update a visual "online" indicator. - Added
src/web/websocket.test.tsto verify protocol upgrades and messaging.