@vocallabs/web-sdk
v1.0.1
Published
Real-time voice communication SDK for browser applications
Readme
@vocallabs/web-sdk
Real-time voice communication SDK for browser applications.
Features
- WebSocket-based bi-directional audio streaming
- Microphone capture and browser audio playback
- Transcript event handling
- Built-in mute controls
- Lightweight event API (
on,off,once) - TypeScript declarations included
Installation
npm install @vocallabs/web-sdkQuick Start
import { Vocallabs } from '@vocallabs/web-sdk';
const client = Vocallabs({
sampleRate: 8000,
connectionTimeout: 5000,
audioFormat: 'audio/x-l16',
});
client.on('connected', () => {
console.log('Connected');
});
client.on('transcript', (payload) => {
console.log('Transcript:', payload.transcript, 'final:', payload.is_final);
});
client.on('error', (err) => {
console.error('SDK error:', err.code, err.message);
});
await client.connect('wss://your-server.example/ws?callId=abc123');
// later
client.disconnect();API
Factory
Vocallabs(options?) => VocalLabsClient
Options
sampleRate:8000 | 16000(default:8000)connectionTimeout: number in ms (default:5000)maxQueueSize: number (default:15)noiseGateThreshold: number in dB (default:-50)speakingThreshold: number (default:0.01)audioFormat:'audio/x-l16' | 'audio/x-mulaw'(default:'audio/x-l16')
Client Methods
connect(wsUrl: string): Promise<void>disconnect(): voiddestroy(): voidmute(): voidunmute(): voidtoggleMute(): booleanclearAudioQueue(): voidsendMessage(message: Record<string, unknown>): voidgetStats(): { sentChunks, receivedChunks, queueSize, readyState }
Client State
isMuted: booleanisConnected: booleanreadyState: 'disconnected' | 'connecting' | 'connected'
Events
connecteddisconnected(payload:{ code?, reason?, wasClean? })error(payload:VocalLabsError)userConnectedtranscript(payload includestranscript,is_final,speaker)muteChanged(payload:{ muted: boolean })audioQueueClearedmessage(unknown server event payload)
Use:
client.on('connected', handler);
client.off('connected', handler);
client.once('connected', handler);Browser Requirements
- Modern browser with
WebSocket,MediaDevices.getUserMedia, andAudioContext - Microphone permission must be granted by the user
- Works best in secure contexts (
https://)
Error Handling
The SDK throws or emits VocalLabsError with code values such as:
INVALID_CONFIGALREADY_CONNECTEDNOT_CONNECTEDCONNECTION_FAILEDCONNECTION_TIMEOUTAUDIO_INIT_FAILEDMICROPHONE_DENIEDAUDIO_PROCESSING_ERRORWEBSOCKET_ERRORMESSAGE_PARSE_ERROR
Development
Build
npm run buildWatch Build
npm run build:watchLicense
MIT
