De Ce WebSocket
WebSocket oferă comunicare bidirectională full-duplex, esențială pentru conversații vocale în timp real. Latență minimă, overhead redus.
Connection Flow
Handshake
HTTP upgrade la WebSocket protocol
Authentication
Token JWT sau API key în header
Configuration
Setări audio: sample rate, encoding
Streaming
Bidirectional audio flow
Message Types
| Type | Direction | Description |
|---|---|---|
| audio_data | Client → Server | Raw audio chunks (PCM/Opus) |
| transcript | Server → Client | Real-time transcription |
| audio_response | Server → Client | TTS audio chunks |
| control | Bidirectional | Start/stop/interrupt signals |
| vad_event | Server → Client | Voice activity detection |
| error | Server → Client | Error messages |
Code Example
// Connect to Kallina WebSocket
const ws = new WebSocket('wss://api.kallina.ai/v1/voice/stream');
ws.onopen = () => {
// Send configuration
ws.send(JSON.stringify({
type: 'config',
audio: {
encoding: 'linear16',
sample_rate: 16000,
channels: 1
},
language: 'ro-RO'
}));
// Start streaming audio from microphone
startAudioCapture();
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'transcript') {
// Show real-time transcription
updateTranscript(data.text, data.is_final);
}
if (data.type === 'audio_response') {
// Play AI response audio
playAudioChunk(data.audio);
}
};Best Practices
Chunk Size
20-50ms audio per chunk (320-800 bytes la 16kHz)
Buffer
Client buffer 100-200ms pentru smooth playback
Heartbeat
Ping every 30s pentru keep-alive
Reconnect
Exponential backoff: 1s, 2s, 4s, 8s, max 30s
Compression
Opus codec pentru bandwidth reduction 50%+
Error Handling
Connection Lost
Retry cu exponential backoff, restaurează context
Audio Gaps
Buffer management, PLC (Packet Loss Concealment)
High Latency
Reduce chunk size, check network quality