JavaScript SDK
The PerfVoice SDK is a single JavaScript file that handles the entire voice agent integration — WebSocket connection, microphone capture, audio encoding, playback, interruptions, and keepalive. No dependencies, no build step.Installation
Add the SDK via a script tag:PerfVoice class globally (or as a UMD/CommonJS module).
Quick Start
Constructor
Methods
voice.start()
Start a voice conversation. Requests microphone permission, opens a WebSocket connection, and begins streaming audio.
Promise<void> that resolves when the connection is established and the agent is ready. Rejects if microphone access is denied or the WebSocket connection fails.
Note: Browsers require a user gesture (click/tap) before allowing microphone access. Always call start() from a button click handler.
voice.stop()
End the conversation immediately. Stops all audio playback, releases the microphone, and closes the WebSocket.
Events
Subscribe to events withvoice.on(event, callback) and unsubscribe with voice.off(event, callback).
transcript
Fired when a transcript is available for either the agent or the user.
connected
Fired when the voice session is established and the agent is ready.
disconnected
Fired when the WebSocket connection closes (either by calling stop() or due to a server-side close).
status
Fired whenever the connection status changes.
error
Fired when an error occurs (microphone denied, WebSocket failure, etc.).
interruption
Fired when the user interrupts the agent (starts speaking while the agent is talking). The SDK automatically stops agent audio playback.
message
Fired for any unhandled message type from the server. Useful for debugging.
Properties
Full Example
A complete example with status indicator, transcript display, and error handling:React Integration
Error Handling
Browser Support
The SDK requires:navigator.mediaDevices.getUserMedia(microphone access)AudioContext(audio playback)WebSocket(real-time communication)
Related
- Voice Agents Overview — Architecture and features
- WebSocket Protocol — Raw WebSocket integration
- Python Integration — Server-side Python integration