Skip to main content
Real-time bi-directional WebSocket for streaming microphone audio and receiving AI voice responses with barge-in support.

Voice WebSocket Endpoint

Input Audio: PCM16LE, Mono, 16 kHz
Output Audio: Chunked MP3, 22.05 kHz

Quick Start

Open WebSocket Connection

Set binaryType to "arraybuffer" for efficient binary audio streaming

Audio Format Specifications

Required Format

Frame Sizes

Formula: bytes = sampleRate × duration × 2Example: 16,000 × 0.020 × 2 = 640 bytes

Message Types

Speech Recognition Events

Partial Transcript

Sent continuously while user is speaking:
Display with visual indication (italic, gray) to show it’s not final
Sent when user stops speaking:
After non-empty final transcript, agent starts processing response

Barge-in Control

Barge-in allows users to interrupt the AI agent mid-speech, creating natural conversation flow.

How It Works

1

User Starts Speaking

While agent is talking, user begins new input
2

Server Detects Speech

Server analyzes if speech is meaningful (not filler words)
3

Server Sends Barge Signal

4

Client Stops Old Audio

Stop all audio players with seq < 12
5

Play New Response

Only play audio matching new seq: 12
Server triggers barge-in when ALL conditions are met:
Filler words do NOT trigger barge-in: uh, um, hmm, eh, ah, ya, yah
Check Sequence on Every Message:
Stop Old Audio Function:
Always maintain currentSeq as a global variable to track the latest sequence number.

Complete Example


Error Handling

Solution: Ensure start message is sent immediately after connection
Solution: Get fresh token from Soca AI dashboard
Server attempts transparent reconnect for audio sends. Client should implement reconnection logic with exponential backoff.

Troubleshooting

Possible causes:
  • Not using MSE for MP3 streaming
  • Missing user-gesture for autoplay
  • Chunks not combined correctly
Solutions:
  • Use <audio> element or Web Audio API
  • Require user click before playing
  • Verify all chunks collected before playing
Cause: Not respecting sequence numbersSolution:
Solutions:
  • Reduce frame size to 20-40 ms
  • Disable heavy DSP in getUserMedia
  • Check network latency
Checklist:
  • ✅ Grant microphone permission
  • ✅ Use HTTPS (required for getUserMedia)
  • ✅ Check browser compatibility
  • ✅ Verify audio constraints (16kHz, mono)

Try It Out

See complete working implementation with source code on GitHub