RealtimeAgent opens a real WebSocket to OpenAI’s Realtime API for bidirectional audio and text.
Looking for token-by-token text streaming (
agent.stream('Tell me a story'))? That moved to Streaming. This page is the voice-first RealtimeAgent.Quick Start
1
Connect and send text
2
Stream microphone audio
How It Works
Both audio and text turns travel over the same live socket.Events
Register handlers by event type, or use'*' to receive every server event. Convenience callbacks mirror the Python agent.
session.created, session.updated, response.text.delta, response.audio.delta, response.done, and error. Any other type the server sends is emitted untouched.
Configuration Options
Passrealtime as a config object to tune voice and audio.
Agent-level options:
llm (default gpt-4o-realtime-preview), apiKey (falls back to OPENAI_API_KEY), url (endpoint override), headers, webSocket (inject a constructor), and connectTimeoutMs (default 30000).
RealtimeAgent API Reference
Full class documentation
Runtime Requirements
The WebSocket implementation is resolved at call time: an injected{ webSocket } constructor first, then globalThis.WebSocket, then the optional ws package.
If no implementation is available,
connect() throws with the three ways to fix it (upgrade Node, install ws, or inject a constructor) — it never pretends to connect.
Best Practices
Always try/catch connect()
Always try/catch connect()
A bad key, a bad URL, or a timeout rejects. Handle the error instead of assuming a connection succeeded.
Send only after connect() resolves
Send only after connect() resolves
sendText, sendAudio, commitAudio, and clearAudio throw when disconnected. Await connect() first, and re-check isConnected() after a possible server close.Handle unknown event types
Handle unknown event types
The server may send event types this SDK doesn’t name. Register
'*' so you see them all rather than dropping frames.Use an ephemeral key in browsers
Use an ephemeral key in browsers
Browsers cannot set handshake headers, so credentials travel as a subprotocol — visible in the page. Mint a short-lived key for that path.
sampleRate is a local playback hint and is not sent to the API. The low-level SQLiteAdapter in praisonai/db/sqlite is unrelated to this agent and still degrades to a Map for legacy callers.Related
Streaming
Token-by-token text streaming
Voice
Voice interactions
Audio
Audio input and output
Agent
Create agents

