Message Overview
The WebSocket API uses two types of messages:Text Messages
JSON-formatted control messages for authentication and status
Binary Messages
Raw audio data in PCM format for real-time streaming
Message Direction
Client → Server
- Authentication Token
- Audio Data
Message Type: Text (String)
Purpose: Authenticate WebSocket connection
Timing: First message after connection establishmentFormat:Validation:
Purpose: Authenticate WebSocket connection
Timing: First message after connection establishmentFormat:
- Must be a valid JWT token
- Token must not be expired
- Token must contain valid device ID
- Must be sent within 30 seconds of connection
Server → Client
- Connection Confirmation
- Error Messages
- Status Messages (Optional)
- Audio Data
Message Type: JSON Text
Purpose: Confirm successful authentication and connectionSchema:Example:Client Action: Begin audio streaming
Purpose: Confirm successful authentication and connectionSchema:
Message Validation
Text Message Validation
JSON Schema Validation
JSON Schema Validation
All text messages must be valid JSON. Invalid JSON will result in immediate connection termination.Valid JSON:Invalid JSON:
Required Fields
Required Fields
All JSON messages must include:
typefield (string)- Additional fields based on message type
Binary Message Validation
Audio Format Validation
Audio Format Validation
Binary messages are validated for:
- Size: Must be divisible by 2 (16-bit samples)
- Range: Sample values within 16-bit signed range (-32768 to 32767)
- Frequency: Should maintain ~16kHz equivalent timing
- 641 bytes (not divisible by 2) ❌
- 0 bytes (empty audio) ❌
-
8KB (too large) ❌
Message Flow Examples
Successful Connection
Error Scenario
Implementation Helpers
- JavaScript
- Flutter/Dart
Debugging Messages
Enable Message Logging
Common Issues
Invalid JSON
Always validate JSON before sending text messages
Audio Format
Ensure audio data matches exact PCM specification
Message Order
Send JWT token first, then wait for confirmation
Connection State
Check WebSocket state before sending messages