Skip to content

OpenAIRealtimeWebSocket

Transport layer that’s handling the connection between the client and OpenAI’s Realtime API via WebSockets. While this transport layer is designed to be used within a RealtimeSession, it can also be used standalone if you want to have a direct connection to the Realtime API.

new OpenAIRealtimeWebSocket(options?): OpenAIRealtimeWebSocket
Parameter Type

options?

OpenAIRealtimeWebSocketOptions

OpenAIRealtimeWebSocket

OpenAIRealtimeBase.constructor

set _tracingConfig(tracingConfig): void

Sets the internal tracing config. This is used to track the tracing config that has been set during the session.create event.

Parameter Type

tracingConfig

null | RealtimeTracingConfig

void

OpenAIRealtimeBase._tracingConfig


get connectionState(): WebSocketState

The current connection state of the WebSocket connection.

WebSocketState


get currentModel(): OpenAIRealtimeModels

The current model that is being used by the transport layer.

OpenAIRealtimeModels

set currentModel(model): void

The current model that is being used by the transport layer. Note: The model cannot be changed mid conversation.

Parameter Type

model

OpenAIRealtimeModels

void

OpenAIRealtimeBase.currentModel


get muted(): null

Always returns null as the WebSocket transport layer does not handle muting. Instead, this should be handled by the client by not triggering the sendAudio method.

null

Whether the input audio track is currently muted null if the muting is not handled by the transport layer

RealtimeTransportLayer.muted

OpenAIRealtimeBase.muted


get status(): "connecting" | "connected" | "disconnected"

The current status of the WebSocket connection.

"connecting" | "connected" | "disconnected"

RealtimeTransportLayer.status

OpenAIRealtimeBase.status

_cancelResponse(): void

Send a cancel response event to the Realtime API. This is used to cancel an ongoing response that the model is currently generating.

void


_interrupt(elapsedTime): void

Do NOT call this method directly. Call interrupt() instead for proper interruption handling.

This method is used to send the right events to the API to inform the model that the user has interrupted the response. It might be overridden/extended by an extended transport layer. See the TwilioRealtimeTransportLayer for an example.

Parameter Type Description

elapsedTime

number

The elapsed time since the response started.

void


close(): void

Close the WebSocket connection.

This will also reset any internal connection tracking used for interruption handling.

void

RealtimeTransportLayer.close

OpenAIRealtimeBase.close


connect(options): Promise<void>

Establishes the connection to the model and keeps the connection alive

Parameter Type Description

options

RealtimeTransportLayerConnectOptions

The options for the connection

Promise<void>

RealtimeTransportLayer.connect

OpenAIRealtimeBase.connect


emit<K>(type, ...args): boolean
Type Parameter

K extends keyof RealtimeTranportEventTypes

Parameter Type

type

K

args

OpenAIRealtimeEventTypes[K]

boolean

RealtimeTransportLayer.emit

OpenAIRealtimeBase.emit


interrupt(): void

Interrupt the ongoing response. This method is triggered automatically by the client when voice activity detection (VAD) is enabled (default) as well as when an output guardrail got triggered.

You can also call this method directly if you want to interrupt the conversation for example based on an event in the client.

void

RealtimeTransportLayer.interrupt

OpenAIRealtimeBase.interrupt


mute(_muted): never

Will throw an error as the WebSocket transport layer does not support muting.

Parameter Type

_muted

boolean

never

RealtimeTransportLayer.mute

OpenAIRealtimeBase.mute


off<K>(type, listener): EventEmitter<EventTypes>
Type Parameter

K extends keyof RealtimeTranportEventTypes

Parameter Type

type

K

listener

(…args) => void

EventEmitter<EventTypes>

RealtimeTransportLayer.off

OpenAIRealtimeBase.off


on<K>(type, listener): EventEmitter<EventTypes>
Type Parameter

K extends keyof RealtimeTranportEventTypes

Parameter Type

type

K

listener

(…args) => void

EventEmitter<EventTypes>

RealtimeTransportLayer.on

OpenAIRealtimeBase.on


once<K>(type, listener): EventEmitter<EventTypes>
Type Parameter

K extends keyof RealtimeTranportEventTypes

Parameter Type

type

K

listener

(…args) => void

EventEmitter<EventTypes>

RealtimeTransportLayer.once

OpenAIRealtimeBase.once


resetHistory(oldHistory, newHistory): void

Reset the history of the conversation. This will create a diff between the old and new history and send the necessary events to the Realtime API to update the history.

Parameter Type Description

oldHistory

RealtimeItem[]

The old history of the conversation.

newHistory

RealtimeItem[]

The new history of the conversation.

void

RealtimeTransportLayer.resetHistory

OpenAIRealtimeBase.resetHistory


sendAudio(audio, options?): void

Send an audio buffer to the Realtime API. This is used for your client to send audio to the model to respond.

Parameter Type Description

audio

ArrayBuffer

The audio buffer to send.

options?

{ commit: boolean; }

The options for the audio buffer.

options.commit?

boolean

void

RealtimeTransportLayer.sendAudio

OpenAIRealtimeBase.sendAudio


sendEvent(event): void

Send an event to the Realtime API. This will stringify the event and send it directly to the API. This can be used if you want to take control over the connection and send events manually.

Parameter Type Description

event

RealtimeClientMessage

The event to send.

void

RealtimeTransportLayer.sendEvent

OpenAIRealtimeBase.sendEvent


sendFunctionCallOutput(
toolCall,
output,
startResponse?): void

Send the output of a function call to the Realtime API.

Parameter Type Description

toolCall

TransportToolCallEvent

The tool call to send the output for.

output

string

The output of the function call.

startResponse?

boolean

Whether to start a new response after sending the output.

void

RealtimeTransportLayer.sendFunctionCallOutput

OpenAIRealtimeBase.sendFunctionCallOutput


sendMessage(message, otherEventData): void

Send a message to the Realtime API. This will create a new item in the conversation and trigger a response.

Parameter Type Description

message

RealtimeUserInput

The message to send.

otherEventData

Record<string, any>

Additional event data to send.

void

RealtimeTransportLayer.sendMessage

OpenAIRealtimeBase.sendMessage


updateSessionConfig(config): void

Updates the session config. This will merge it with the current session config with the default values and send it to the Realtime API.

Parameter Type Description

config

Partial<RealtimeSessionConfig>

The session config to update.

void

RealtimeTransportLayer.updateSessionConfig

OpenAIRealtimeBase.updateSessionConfig