Overview
RestAI uses Bun’s native WebSocket implementation combined with Redis Pub/Sub to deliver real-time updates across the system. This enables instant notifications for orders, table status changes, and kitchen operations.Architecture
WebSocket Manager
The WebSocket manager handles client connections, room subscriptions, and message broadcasting.Message Handlers
WebSocket messages are handled based on their type.apps/api/src/ws/handlers.ts
Room-Based Broadcasting
RestAI uses a room-based architecture for targeted message delivery.Room Types
- Branch Rooms
- Table Rooms
- Session Rooms
Pattern:
branch:{branch_id}Subscribers:- All staff assigned to the branch
- Kitchen displays
- POS terminals
- New orders created
- Order status changes
- Table status updates
- Menu item availability changes
Client Integration
React Hook for WebSocket
Publishing Events from API
When data changes on the server, publish WebSocket events to notify connected clients.Event Types
Order Events
Table Events
Session Events
Redis Configuration
apps/api/src/lib/redis.ts
Horizontal Scaling
Redis Pub/Sub enables multiple API server instances to share WebSocket events:Error Handling & Resilience
The system gracefully degrades if Redis is unavailable. Events are broadcast locally to clients connected to the same server instance.
Best Practices
- Always Authenticate: Verify JWT tokens before joining rooms
- Room Scoping: Only join rooms the user has permission to access
- Heartbeat/Ping: Send periodic ping messages to keep connections alive
- Reconnection Logic: Implement exponential backoff for reconnections
- Event Versioning: Include version/timestamp in events for idempotency
- Clean Disconnects: Always unsubscribe from rooms on disconnect
Monitoring
Next Steps
System Overview
Learn about the overall system architecture
Database Schema
Understand the data model and relationships
API Reference
Explore REST API endpoints
Authentication
JWT authentication and WebSocket auth flow