Overview
HAPI Hub uses Socket.IO for bidirectional real-time communication between the CLI and hub.Namespace
CLI connections use the/cli namespace:
Connection Process
1. CLI Connects to Hub
2. Authentication
Authentication is performed via theauth object in the handshake:
3. Rooms
When authenticated, the socket automatically joins rooms:session:<sessionId>- Receives updates for this sessionmachine:<machineId>- Receives updates for this machine
Connection Example
Event Flow
CLI to Hub
Hub to CLI
Hub to Web
Web clients receive updates via:- SSE (
GET /api/events) - One-way events - REST API - Request/response
Error Handling
The hub emitserror events for:
Access Errors
Error Structure
Reconnection
Socket.IO automatically handles reconnection:Custom Reconnection Logic
Heartbeat
The CLI should send periodic heartbeats:Session Heartbeat
Machine Heartbeat
Ping/Pong
Test connection latency:Transport Options
Socket.IO supports multiple transports:WebSocket Only (Recommended)
WebSocket with Polling Fallback
CORS Configuration
Configure CORS for Socket.IO connections:- Same-origin requests
- Origins derived from
HAPI_PUBLIC_URL - Origins in
CORS_ORIGINS
Security
Namespace Isolation
All operations are scoped to the authenticated namespace:- Sessions and machines are filtered by namespace
- Cross-namespace access returns
access-deniederror - Each namespace is isolated (multi-tenant)
No Token Auth
Socket.IO connections don’t use JWT tokens. Instead:- CLI connects with
namespacein auth - Hub validates namespace exists
- CLI can only access resources in its namespace
Session/Machine Ownership
The hub verifies:- Session belongs to the namespace
- Machine belongs to the namespace
- No cross-namespace data leakage
Next Steps
Socket Events
Detailed event documentation
RPC
Remote procedure calls