Introduction
The Justina Backend provides WebSocket endpoints for real-time bidirectional communication between clients and the server. WebSockets enable:- Live telemetry streaming from surgical simulations
- Instant notifications to AI systems when surgeries complete
- Low-latency data transfer for responsive user experiences
WebSocket Base URL
All WebSocket connections are established at:Available Channels
| Endpoint | Purpose | Required Role |
|---|---|---|
/ws/simulation | Stream surgical telemetry data in real-time | ROLE_SURGEON |
/ws/ai | Receive surgery completion notifications | ROLE_AI |
Authentication
WebSocket connections require JWT authentication via query parameter:Why Query Parameters?
Unlike HTTP requests, WebSocket handshakes don’t easily support custom headers. The token is passed as a query parameter and validated during the handshake phase before the connection is established.Authentication Flow
Store Attributes
Save user information in WebSocket session attributes:
SURGEON_ID- User’s UUIDUSERNAME- User’s usernameROLE- User’s role (ROLE_SURGEON or ROLE_AI)
Connection Examples
Connection Security
Token Validation
The handshake interceptor validates:- Token signature (HMAC-SHA256)
- Token expiration (24-hour validity)
- Token issuer (
Justina_Backend) - Required claims (
userId,role)
Role-Based Access
Connections are restricted by role:/ws/simulation- OnlyROLE_SURGEONcan connect/ws/ai- OnlyROLE_AIcan connect
POLICY_VIOLATION closure.
WebSocket Close Status Codes
| Code | Reason | Description |
|---|---|---|
1000 | Normal Closure | Connection closed cleanly |
1002 | Protocol Error | WebSocket protocol violation |
1003 | Bad Data | Invalid message format (e.g., malformed JSON) |
1008 | Policy Violation | Authentication failure or insufficient permissions |
1011 | Internal Error | Server-side error during message processing |
Message Format
All WebSocket messages use JSON format. Each channel has specific message schemas:Simulation Channel
See Simulation Channel for telemetry message formats.AI Channel
See AI Channel for notification message formats.Connection Lifecycle
Error Handling
Authentication Errors
Message Validation Errors
Best Practices
CORS Configuration
WebSocket endpoints allow connections from all origins (*). For production deployments, configure specific allowed origins in WebSocketConfig.java:
Troubleshooting
Connection Immediately Closes
Cause: Invalid or missing JWT token Solution: Verify token is included in URL and hasn’t expired”POLICY_VIOLATION” Error
Cause: Insufficient role permissions or authentication failure Solution: Ensure token has the correct role for the channelMessage Not Received
Cause: Invalid JSON format or missing required fields Solution: Validate message structure against channel documentationNext Steps
Simulation Channel
Stream telemetry data from surgical simulations
AI Channel
Receive real-time surgery completion notifications