Implementation:Openai Openai node OpenAIRealtimeWS
| Knowledge Sources | |
|---|---|
| Domains | Realtime, WebSocket |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for establishing WebSocket connections to the OpenAI Realtime API provided by the openai-node SDK.
Description
OpenAIRealtimeWS is the Node.js WebSocket implementation for the Realtime API, using the ws library. OpenAIRealtimeWebSocket is the browser implementation using the native WebSocket API. Both share a common base (OpenAIRealtimeEmitter) for typed event dispatch.
The create() factory method is the primary entry point. It constructs the WebSocket URL (via buildRealtimeURL), establishes the connection with authentication headers, and returns a ready-to-use instance.
Usage
Use OpenAIRealtimeWS in Node.js environments and OpenAIRealtimeWebSocket in browser environments. The create() factory handles connection setup.
Code Reference
Source Location
- Repository: openai-node
- File: src/realtime/ws.ts (Node.js), src/realtime/websocket.ts (Browser)
- Lines: ws.ts:L6-113, websocket.ts:L21-157, internal-base.ts:L86-98 (buildRealtimeURL)
Signature
// Node.js
export class OpenAIRealtimeWS extends OpenAIRealtimeEmitter {
socket: WS.WebSocket;
constructor(props: {
client: Pick<OpenAI, 'apiKey' | 'baseURL' | '_callApiKey'>;
model: string;
options?: WS.ClientOptions;
});
static async create(
client: Pick<OpenAI, 'apiKey' | 'baseURL' | '_callApiKey'>,
props: { model: string; options?: WS.ClientOptions },
): Promise<OpenAIRealtimeWS>;
send(event: RealtimeClientEvent): void;
close(props?: { code: number; reason: string }): void;
}
// Browser
export class OpenAIRealtimeWebSocket extends OpenAIRealtimeEmitter {
socket: WebSocket;
static async create(
client: Pick<OpenAI, 'apiKey' | 'baseURL' | '_callApiKey'>,
props: { model: string; dangerouslyAllowBrowser?: boolean },
): Promise<OpenAIRealtimeWebSocket>;
send(event: RealtimeClientEvent): void;
close(props?: { code: number; reason: string }): void;
}
Import
import { OpenAIRealtimeWS } from 'openai/realtime/ws';
import { OpenAIRealtimeWebSocket } from 'openai/realtime/websocket';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| client | 'baseURL' | '_callApiKey'> | Yes | OpenAI client instance or subset |
| model | string | Yes | Model ID (e.g., 'gpt-4o-realtime-preview') |
| options (Node.js) | WS.ClientOptions | No | WebSocket connection options |
| dangerouslyAllowBrowser | boolean | No (browser only) | Required for browser usage |
Outputs
| Name | Type | Description |
|---|---|---|
| instance | OpenAIRealtimeWebSocket | Connected WebSocket instance with send/on/close interface |
Usage Examples
Node.js Connection
import OpenAI from 'openai';
import { OpenAIRealtimeWS } from 'openai/realtime/ws';
const client = new OpenAI();
const rt = await OpenAIRealtimeWS.create(client, {
model: 'gpt-4o-realtime-preview',
});
rt.on('session.created', (event) => {
console.log('Session created:', event.session.id);
});
rt.on('error', (event) => {
console.error('Error:', event.error);
});
Browser Connection
import OpenAI from 'openai';
import { OpenAIRealtimeWebSocket } from 'openai/realtime/websocket';
const client = new OpenAI({ dangerouslyAllowBrowser: true });
const rt = await OpenAIRealtimeWebSocket.create(client, {
model: 'gpt-4o-realtime-preview',
dangerouslyAllowBrowser: true,
});