WebSocket client for OpenAI Realtime API.
This module manages the WebSocket connection to OpenAI's Realtime API, handling connection lifecycle, message parsing, and event dispatching.
Architecture
The WebSocket client operates as a separate process that:
- Establishes and maintains the WebSocket connection
- Parses incoming JSON messages into structured events
- Forwards events to the session process and any registered listeners
- Sends outgoing messages (audio, user input, tool outputs, etc.)
Usage
This module is typically not used directly. Instead, use Codex.Realtime.Session
which manages the WebSocket connection internally.
{:ok, ws} = OpenAIWebSocket.start_link(
session_pid: self(),
config: %ModelConfig{api_key: "sk-..."},
model_name: "gpt-4o-realtime-preview"
)
OpenAIWebSocket.send_message(ws, %{"type" => "response.create"})
Summary
Functions
Add a listener for events.
Gracefully close the websocket connection.
Remove a listener.
Send a raw websocket frame through the underlying WebSockex client.
Send a message to the WebSocket.
Start the WebSocket connection.
Types
@type send_frame_error() :: %WebSockex.ConnError{__exception__: true, original: term()} | %WebSockex.FrameEncodeError{ __exception__: true, close_code: term(), frame_payload: term(), frame_type: term(), reason: term() } | %WebSockex.InvalidFrameError{__exception__: true, frame: term()} | %WebSockex.NotConnectedError{__exception__: true, connection_state: term()}
@type t() :: %Codex.Realtime.OpenAIWebSocket{ config: Codex.Realtime.Config.ModelConfig.t(), listeners: [pid()], session_pid: pid() }
Functions
Add a listener for events.
Listeners receive {:model_event, event} messages for all events.
@spec close(pid()) :: :ok
Gracefully close the websocket connection.
WebSockex does not support sending close frames via send_frame/2; the
websocket process has to initiate shutdown from its own callback context.
Remove a listener.
@spec send_frame(pid(), outbound_frame()) :: :ok | {:error, send_frame_error()}
Send a raw websocket frame through the underlying WebSockex client.
@spec send_message(pid(), map() | [map()]) :: :ok | {:error, send_frame_error()}
Send a message to the WebSocket.
Accepts a single message map or a list of message maps.
Examples
OpenAIWebSocket.send_message(ws, %{"type" => "response.create"})
OpenAIWebSocket.send_message(ws, [
%{"type" => "input_audio_buffer.append", "audio" => base64_audio},
%{"type" => "input_audio_buffer.commit"}
])
Start the WebSocket connection.
Options
:session_pid- Required. The session process to receive events.:config- Required. The model configuration.:model_name- Optional model name to use in the URL.
Returns
{:ok, pid}on successful connection{:error, reason}on failure