跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

voice.connect()

The connect() method establishes a WebSocket or WebRTC connection for real-time speech-to-speech communication. This method must be called before using other real-time features like send() or answer().

Usage example
Usage example的直接链接

import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime'
import Speaker from '@mastra/node-speaker'

const speaker = new Speaker({
sampleRate: 24000,
channels: 1,
bitDepth: 16,
})

const voice = new OpenAIRealtimeVoice({
apiKey: process.env.OPENAI_API_KEY,
speaker: 'alloy',
})
// Connect to the real-time service
await voice.connect()
// Now you can use real-time features
voice.on('speaker', stream => {
stream.pipe(speaker)
})

Parameters
Parameters的直接链接

options?:

Record<string, unknown>
Provider-specific connection options

Return value
Return value的直接链接

Returns a Promise<void> that resolves when the connection is successfully established.

Provider-specific options
Provider-specific options的直接链接

Connection configuration depends on the real-time voice provider.

OpenAI Realtime
OpenAI Realtime的直接链接

connect() accepts an optional requestContext for tool execution:

options.requestContext?:

RequestContext
Runtime context passed to tools called during the session.

See Request context for how to populate runtime values.

Set connectTimeoutMs in the OpenAIRealtimeVoice constructor, not in the connect() call:

connectTimeoutMs?:

number
= 15000
Connection handshake deadline in milliseconds. Must be a positive, finite number no greater than 2,147,483,647. Applies only to connection setup, not to an established session.

Connection failures
Connection failures的直接链接

For OpenAI Realtime, connect() waits for both the WebSocket to open and the server to create a session. It rejects if the connection fails, the server reports an error during the handshake, or the socket closes before the session is ready. A silent handshake times out after 15,000 milliseconds by default.

Catch connection failures directly. An error event listener doesn't replace handling the rejected promise:

import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime'

const voice = new OpenAIRealtimeVoice({
apiKey: process.env.OPENAI_API_KEY,
connectTimeoutMs: 30_000,
})

try {
await voice.connect()
} catch (error) {
console.error('Could not connect to the realtime service:', error)
}

A failed handshake closes its socket and clears its pending waits. You can retry with connect() on the same instance.

Using with CompositeVoice
using-with-compositevoice的直接链接

When using CompositeVoice, the connect() method forwards its options to the configured real-time provider. It throws if no real-time provider is configured:

import { CompositeVoice } from '@mastra/core/voice'
import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime'
const realtimeVoice = new OpenAIRealtimeVoice()
const voice = new CompositeVoice({
realtime: realtimeVoice,
})
// This will use the OpenAIRealtimeVoice provider
await voice.connect()

Notes
Notes的直接链接

  • This method is only implemented by real-time voice providers that support speech-to-speech capabilities
  • Providers that inherit the base connect() implementation log a debug message and resolve without establishing a connection
  • The connection must be established before using other real-time methods like send() or answer()
  • When you're done with the voice instance, call close() to properly clean up resources
  • Some providers may automatically reconnect on connection loss, depending on their implementation
  • Connection errors will typically be thrown as exceptions that should be caught and handled