WebSocket
Real-time emotion updates via persistent WebSocket connections.
WebSocket
The WebSocket endpoint provides a persistent, bidirectional connection for real-time emotion updates. Instead of polling the REST API after each interaction, maintain a WebSocket connection to receive instant state changes and server-pushed proactive actions.
Built on Cloudflare Durable Objects with the WebSocket Hibernation API, idle connections have zero compute cost.
Connect
Establish a WebSocket connection for a session. Authentication is provided via the token query parameter since the WebSocket protocol does not support custom headers.
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
token | string | Yes | Your API key |
Path Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | The session ID to connect to |
Connection Example
JavaScript
Python
Client to Server Messages
All messages are JSON objects with a type field.
Simplified Turn
Send a text message for emotion processing:
Full Turn
Send a turn with full event details and optional timing:
Appraisal-Based Turn
Provide a pre-computed appraisal vector instead of a text description:
Request State
Request a full state snapshot:
Set Proactive Preferences
Update proactive behavior preferences for the session:
Acknowledge Proactive Action
Acknowledge receipt of a proactive action to prevent re-delivery:
Server to Client Messages
All messages are JSON objects with a type field.
Connected
Sent immediately after the WebSocket connection is established:
Turn Result
Returned after processing a turn or appraisal-based turn:
State
Full state snapshot, returned in response to get_state:
Proactive
Server-pushed proactive action (requires proactive behavior to be enabled):
Missed Proactive
Queued actions delivered on reconnect if proactive actions occurred while the client was disconnected:
Full Example
A complete example showing turn processing and proactive action handling over WebSocket:
JavaScript
Python
Connection Details
| Property | Value |
|---|---|
| Protocol | wss:// (TLS required) |
| Authentication | Query parameter token (custom headers not supported) |
| Idle cost | Zero (WebSocket Hibernation API on Durable Objects) |
| Reconnection | Missed proactive actions are queued and delivered on reconnect |
| Message format | JSON |
Errors
WebSocket errors are delivered as JSON messages:
| Code | Description |
|---|---|
UNAUTHORIZED | Invalid or missing token |
NOT_FOUND | Session not found |
VALIDATION_ERROR | Invalid message format |
RATE_LIMIT | Too many messages |