Low-Latency Streaming: WebSockets, WebRTC, and Audio Protocols
Understand the networking protocols and audio codecs required to stream voice bidirectionally with sub-500ms response times.
Why traditional HTTP request-response fails for voice
Standard HTTP POST endpoints operate on complete payloads: the user must finish speaking, wait for audio compression, send the entire recording, and wait for the complete server response before playback begins. This half-duplex interaction feels like a walkie-talkie. Conversational voice requires full-duplex communication: both client and server continuously stream audio frames simultaneously in real time.
WebSockets vs. WebRTC for real-time voice sessions
Two primary protocols power real-time voice AI: WebSockets and WebRTC. WebSockets run over TCP and provide a reliable, bidirectional, ordered byte stream that is simple to implement and inspect. However, TCP packet retransmission can introduce latency spikes on jittery mobile networks. WebRTC runs over UDP and was designed specifically for real-time media: it prioritizes immediate frame delivery over lossless delivery, making it the industry gold standard for sub-300ms peer-to-server audio sessions.
// Capturing and streaming browser microphone frames to realtime endpoint
const ws = new WebSocket('wss://api.provider.com/v1/realtime-audio');
const audioContext = new AudioContext({ sampleRate: 24000 });
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
const source = audioContext.createMediaStreamSource(stream);
const processor = audioContext.createScriptProcessor(2048, 1, 1);
processor.onaudioprocess = (e) => {
const float32Data = e.inputBuffer.getChannelData(0);
const int16Data = convertFloat32ToInt16(float32Data); // Raw PCM
if (ws.readyState === WebSocket.OPEN) {
ws.send(int16Data.buffer);
}
};
source.connect(processor);
processor.connect(audioContext.destination);
});Audio codecs: PCM, Opus, and sample rates
Audio data must be formatted for streaming efficiency. Raw linear PCM (Pulse-Code Modulation, typically 16-bit at 16kHz or 24kHz) requires zero encoding latency and is universally understood by model backends, but consumes substantial bandwidth (~384 kbps). The Opus codec offers an outstanding compromise: high-fidelity voice compression at low bitrates (16-32 kbps) with minimal algorithmic latency (5-20ms), making it ideal for browser and mobile network transmissions.
Managing network jitter, packet loss, and audio buffers
Real-world mobile connections experience packet loss and jitter. Client audio players must implement a jitter buffer: a tiny dynamic queue (e.g., 40-80ms of audio) that smooths playback without introducing noticeable lag. If network conditions degrade, adaptive bitrates and packet loss concealment (PLC) prevent audible pops, clicks, or robotic stuttering.
Bi-directional client-server audio framing
During an active session, the client microphone captures audio in small chunks (e.g., 20ms or 100ms frames) and streams them continuously to the server. Simultaneously, the server streams back incoming synthesized audio frames, which the client feeds into a streaming audio worklet (in web browsers) or audio queue (in mobile apps) for immediate playback.
Practical exercise
Inspect the network tab of a modern browser while using a realtime voice assistant. Identify whether it communicates over a WebSocket (wss://) or a WebRTC DataChannel/MediaStream, and calculate the size and frequency of the audio chunks being dispatched every second.
Sources and further reading
These primary or specialist references informed the concepts in this guide. Product details can change, so verify current documentation before implementation.