docs/src/content/en/reference/voice/voice.off.mdx
The off() method removes event listeners previously registered with the on() method. This is particularly useful for cleaning up resources and preventing memory leaks in long-running applications with real-time voice capabilities.
import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime'
import chalk from 'chalk'
// Initialize a real-time voice provider
const voice = new OpenAIRealtimeVoice({
realtimeConfig: {
model: 'gpt-5.1-realtime',
apiKey: process.env.OPENAI_API_KEY,
},
})
// Connect to the real-time service
await voice.connect()
// Define the callback function
const writingCallback = ({ text, role }) => {
if (role === 'user') {
process.stdout.write(chalk.green(text))
} else {
process.stdout.write(chalk.blue(text))
}
}
// Register event listener
voice.on('writing', writingCallback)
// Later, when you want to remove the listener
voice.off('writing', writingCallback)
<PropertiesTable content={[ { name: 'event', type: 'string', description: "Name of the event to stop listening for (e.g., 'speaking', 'writing', 'error')", isOptional: false, }, { name: 'callback', type: 'function', description: 'The same callback function that was passed to on()', isOptional: false, }, ]} />
This method doesn't return a value.
off() must be the same function reference that was passed to on()