Implementation:Openai Openai node Beta RealtimeBase
| Knowledge Sources | |
|---|---|
| Domains | SDK, Realtime |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The OpenAIRealtimeEmitter abstract base class provides the shared event handling, error management, and URL construction logic for all beta realtime WebSocket clients.
Description
This module defines the foundational types and classes used by both browser-based (OpenAIRealtimeWebSocket) and Node.js-based (OpenAIRealtimeWS) realtime client implementations. The core abstraction is OpenAIRealtimeEmitter, an abstract class extending the SDK's internal EventEmitter that is parameterized with strongly-typed realtime event mappings.
The OpenAIRealtimeEmitter class declares two abstract methods -- send() for transmitting client events and close() for terminating the connection -- which concrete subclasses must implement. It also provides a protected _onError() method that normalizes error handling: if an error listener is registered, the error is emitted through the event system; otherwise, it creates an unhandled promise rejection with a helpful message guiding developers to register an error callback.
The module also exports OpenAIRealtimeError (a subclass of OpenAIError carrying server error data and event IDs), the isAzure() type guard for detecting Azure clients, and buildRealtimeURL() which constructs the WSS URL with appropriate query parameters for both standard OpenAI and Azure OpenAI endpoints.
Usage
This module is not used directly by application developers. It serves as the internal base layer that OpenAIRealtimeWebSocket and OpenAIRealtimeWS extend. Developers working with the realtime API should use one of those concrete subclasses instead.
Code Reference
Source Location
- Repository: openai-node
- File: src/beta/realtime/internal-base.ts
Signature
export class OpenAIRealtimeError extends OpenAIError {
error?: ErrorEvent.Error | undefined;
event_id?: string | undefined;
constructor(message: string, event: ErrorEvent | null);
}
export abstract class OpenAIRealtimeEmitter extends EventEmitter<RealtimeEvents> {
abstract send(event: RealtimeClientEvent): void;
abstract close(props?: { code: number; reason: string }): void;
protected _onError(event: ErrorEvent | null, message?: string, cause?: any): void;
}
export function isAzure(client: Pick<OpenAI, 'apiKey' | 'baseURL'>): client is AzureOpenAI;
export function buildRealtimeURL(client: Pick<OpenAI, 'apiKey' | 'baseURL'>, model: string): URL;
Import
// Internal import (not part of public API)
import { OpenAIRealtimeEmitter, OpenAIRealtimeError, buildRealtimeURL, isAzure } from 'openai/beta/realtime/internal-base';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| event | RealtimeClientEvent |
Yes | The client event to send via send(). Must conform to the Realtime API event schema.
|
| props | { code: number; reason: string } |
No | Optional close code and reason for close(). Defaults vary by subclass (typically 1000 / "OK").
|
Outputs
| Name | Type | Description |
|---|---|---|
| "event" emission | RealtimeServerEvent |
All server events are emitted under the generic event key, as well as their specific type key (e.g. session.created).
|
| "error" emission | OpenAIRealtimeError |
Error events from the server or connection failures are emitted under the error key.
|
| buildRealtimeURL return | URL |
A WSS URL targeting the realtime API, with model (or deployment and api-version for Azure) as query parameters.
|
Usage Examples
// buildRealtimeURL usage (internal)
import OpenAI from 'openai';
import { buildRealtimeURL } from 'openai/beta/realtime/internal-base';
const client = new OpenAI();
const url = buildRealtimeURL(client, 'gpt-4o-realtime-preview');
// => wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview
// OpenAIRealtimeEmitter is used by subclasses
// Concrete usage: see OpenAIRealtimeWebSocket or OpenAIRealtimeWS