Reference

LiveSession

Stateful realtime WebSocket session

Create a session through ProsodyClient:

const session = prosody.stream.session(options);

Methods

class LiveSession {
  start(options?: LiveSessionStartOptions): Promise<void>;

  send(
    chunk: ArrayBuffer | Uint8Array | Buffer | Int16Array,
  ): void;

  waitForFrame(timeoutMs?: number): Promise<void>;

  stop(options?: {
    waitForSessionEndMs?: number;
  }): Promise<SessionEndEvent | null>;

  close(): void;
  snapshot(options?: TranscribeOptions): Transcription;
}

Properties

readonly conversation: Conversation;
readonly sessionId: string | null;
readonly started: boolean;
readonly readyState: number;
readonly latestDirective: DirectiveEvent | null;
readonly speakerProfiles: SpeakerProfile[];
readonly sessionEnd: SessionEndEvent | null;

Options

interface LiveSessionStartOptions {
  source?: string;
  sourceOffsetMs?: number;
  sampleRate?: number;
  encoding?: 'pcm16' | 'linear16' | 'opus';
  container?: 'ogg' | 'webm';
  analysisMode?: string;
  chunkSeconds?: number;
  sessionId?: string;
  capabilities?: ('transcription' | 'diarization' | 'acoustic_state')[];
}

capabilities names what the session does with its audio. Leave it out and the session runs everything. Name a subset and the session runs only that: ['transcription'] returns words and nothing else, and ['transcription', 'diarization'] adds who spoke when. A session without acoustic_state is not run through the acoustic model at all, so it carries no acoustic readings, speaker identity or memory events, and it costs less to serve. That is the right choice for telephone audio. The acknowledgement the session receives when it opens echoes the capabilities it runs.

prosody.stream.session() additionally accepts these fields of LiveSessionOptions:

onUpdate?: (session: LiveSession) => void;
onError?: (error: Error) => void;
onClose?: (code: number, reason: string) => void;
onEvent?: (event: ProsodyEvent | Record<string, unknown>) => void;
WebSocketImpl?: typeof WebSocket;

Lifecycle

const session = prosody.stream.session({
  encoding: 'pcm16',
  sampleRate: 16_000,
  onUpdate: (current) => render(current.snapshot()),
});

await session.start();

for (const chunk of chunks) {
  session.send(chunk);
  await session.waitForFrame();
}

const finalEvent = await session.stop();
const result = session.snapshot();

start() resolves after config_ack. stop() sends the end frame, waits up to 20 seconds for session_end, closes the socket, and returns the final event. Pass { waitForSessionEndMs: 0 } for an immediate stop.

close() closes the socket immediately.

Snapshot

const current: Transcription = session.snapshot();

snapshot() derives Transcription from the current Conversation. Each snapshot reflects the speaker updates committed so far. Resolved identity for each speaker is on session.speakerProfiles.

Backpressure

waitForFrame() resolves on the next directive or frame_ack. The default timeout is 10 seconds. Use it to pace file replay against the server's analysis cadence.

On this page