WebSocket API¶
Real-time communication via WebSocket connections.
Endpoints¶
Agent WebSocket¶
Authentication: API key in initial message
Purpose: Agent connection for metrics and commands
Frontend Metrics WebSocket¶
Authentication: JWT token in initial message
Purpose: Real-time metrics updates for frontend
Frontend Logs WebSocket¶
Authentication: JWT token in initial message
Purpose: Real-time log streaming for frontend
Agent WebSocket Protocol¶
Connection¶
- Connect to
/api/v1/agents/ws - Send authentication message:
- Receive authentication response:
Sending Metrics¶
Send metrics every 5 seconds:
{
"type": "metrics",
"server_id": "uuid",
"cpu_percent": 45.2,
"memory_percent": 62.5,
"disk_usage": [...],
"network": [...],
"timestamp": "2024-01-01T00:00:00Z"
}
Receiving Commands¶
Sending Command Response¶
{
"type": "command_response",
"command_id": "uuid",
"output": "total 100\n...",
"exit_code": 0,
"duration_ms": 150
}
Docker Commands¶
Receive:
{
"type": "docker_command",
"command_id": "uuid",
"action": "start",
"container_id": "container-id"
}
Respond:
{
"type": "docker_response",
"command_id": "uuid",
"success": true,
"message": "Container started"
}
Frontend WebSocket Protocol¶
Metrics WebSocket¶
- Connect to
/api/v1/ws/metrics/{server_id} - Send authentication:
- Receive metrics updates:
Logs WebSocket¶
- Connect to
/api/v1/ws/logs/{server_id} - Send authentication:
- Receive log entries:
Message Types¶
Agent Messages¶
auth: Authentication requestauth_success: Authentication confirmationmetrics: Metrics datacommand: Command execution requestcommand_response: Command execution resultdocker_command: Docker operation requestdocker_response: Docker operation result
Frontend Messages¶
auth: Authentication requestmetrics: Metrics updatelog: Log entryalert: Alert notificationerror: Error message
Error Handling¶
Authentication Failed¶
Invalid Message¶
Reconnection¶
Both agents and frontend clients should implement automatic reconnection:
- Detect connection loss
- Wait for exponential backoff
- Reconnect and re-authenticate
- Resume normal operation
Best Practices¶
- Always authenticate first before sending other messages
- Handle reconnections gracefully
- Validate message types before processing
- Implement heartbeat to detect connection issues
- Use TLS/SSL in production