WebSocket Real-time
Set up persistent WebSocket connections for live emotion updates.
WebSocket Real-time
For real-time applications like chatbots, games, and interactive experiences, WebSocket connections provide lower latency and server-pushed updates compared to REST polling. This guide walks through connecting, sending turns, handling proactive actions, and reconnection.
Connection Setup
Open a WebSocket connection by providing your session ID and API key:
JavaScript
Connection URL Format
| Parameter | Description |
|---|---|
sessionId | The session ID to connect to |
token | Your API key (mk_live_ or mk_test_) |
On successful connection, the server sends a connected message:
Sending Turns
Send a turn through the WebSocket by sending a JSON message with type: "turn":
The server responds with a turn_result message containing the same data as the REST POST /v1/turn endpoint:
Sending Appraisals
You can also send direct appraisal vectors over WebSocket:
Proactive Actions
molroo's emotion engine can push proactive actions when the character's internal state triggers behavior without user input -- for example, when stress builds up or a need goes unmet.
Enabling Proactive Behavior
Proactive behavior is configured at the session level. When enabled, the server pushes proactive messages:
Handling Proactive Actions
Acknowledging Proactive Actions
After handling a proactive action, acknowledge it so the engine knows it was delivered:
Reconnection
Connections can drop due to network issues, server restarts, or client inactivity. Implement automatic reconnection with exponential backoff:
Missed Proactive Actions
When you reconnect after a disconnection, any proactive actions that fired while the client was offline are automatically delivered as a missed_proactive message:
This ensures no character-initiated events are lost during connection gaps.
Heartbeat
The server sends periodic ping frames. The WebSocket protocol handles pong responses automatically in browsers. If you are using a server-side WebSocket client, make sure your library responds to pings:
If the server does not receive a pong within 30 seconds, the connection is terminated.
Complete Example
A full real-time chat integration with reconnection and proactive handling:
When to Use WebSocket vs REST
| Use Case | Recommended | Why |
|---|---|---|
| Real-time chatbots | WebSocket | Low latency, server-pushed proactive actions |
| Games with NPCs | WebSocket | Frequent updates, persistent connection |
| Interactive experiences | WebSocket | Bidirectional communication, live emotion updates |
| Batch processing | REST | Stateless, no persistent connection needed |
| Serverless functions | REST | Functions are ephemeral, cannot hold connections |
| Simple integrations | REST | Easier to implement, fewer moving parts |
| Infrequent interactions | REST | No need to maintain a connection |
Rule of thumb: If your user is actively interacting and expects real-time feedback, use WebSocket. If you are processing events in the background or on a schedule, use REST.