Overview
The WebSocket channel provides a lightweight alternative to WebRTC for streaming audio to and from Iqra AI agents. WebSocket integration is ideal for:- Custom telephony integrations
- Browser applications without WebRTC support
- Server-to-server audio streaming
- IoT devices with limited codec support
- Simplified deployment without peer-to-peer negotiation
Architecture
Connection model
Key difference from WebRTC: WebSocket is a simple bidirectional channel without SDP negotiation, ICE candidates, or RTP encapsulation.Transport implementation
TheWebSocketClientTransport handles both text and binary messages:
- Binary messages: Raw audio frames (PCM, μ-law, A-law, etc.)
- Text messages: Control signals, metadata, transcripts
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:8
Session initialization
1
Request web session
Client initiates a session with
transportType: 'WebSocket':2
Connect WebSocket
Establish WebSocket connection to the provided URL:
3
Backend activates transport
When the WebSocket connects, backend activates the deferred transport:Source:
IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:2714
Conversation begins
AI agent starts speaking, and audio flows bidirectionally through the WebSocket.
Audio streaming
Sending audio (Client → Backend)
Stream microphone audio as binary WebSocket frames:Receiving audio (Backend → Client)
Play AI agent’s voice from binary frames:Backend audio handling
The backend transport processes both directions:IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:32
Sending audio from backend
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:85
Audio format configuration
Session configuration
Specify audio encoding in the session request:Supported codecs
- PCMU (μ-law)
- PCMA (A-law)
- Linear PCM
- OPUS
G.711 μ-law encoding - Standard for North American telephony
- Sample rate: 8000 Hz
- Bitrate: 64 kbps
- Frame size: 160 samples @ 20ms
- Use case: Telephony integration, VoIP
Text messaging
WebSocket supports bidirectional text messages for control and metadata:Client → Backend
Backend → Client
Backend implementation
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:95
Connection lifecycle
Connection establishment
Graceful disconnection
Client-initiated disconnect:IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:106
Handling disconnects
IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:45
Security and authentication
Token-based authentication
WebSocket URL includes an HMAC token:IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:250
Token generation
IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:209
TLS encryption: Always use
wss:// (WebSocket Secure) in production to encrypt audio and text data.Server-to-server integration
WebSocket is ideal for backend services streaming audio:Python example
Node.js example
Performance considerations
Frame size matters: 20ms frames (160 samples @ 8kHz) balance latency and network overhead. Smaller frames = lower latency but more packet overhead.
Buffering: Implement jitter buffer on client side to smooth out network variations:
Backpressure: Monitor WebSocket bufferedAmount to avoid overwhelming the connection:
Troubleshooting
No audio received
Symptom: Connected but no audio from AI agent Checks:- Verify
binaryTypeis set to'arraybuffer' - Check audio format matches session configuration
- Confirm
onmessagehandler processes binary messages - Look for errors in backend logs
Choppy audio playback
Symptom: Audio plays but sounds garbled Solutions:- Implement jitter buffer
- Check network latency/packet loss
- Verify sample rate matches Web Audio Context
- Ensure audio frames are played in order
Connection drops
Symptom: WebSocket closes unexpectedly Common causes:- Token expired (5 minute limit)
- Network interruption
- Backend server restart
- Session timeout
Comparison: WebSocket vs WebRTC
When to choose WebSocket:
- Backend service integration
- Simpler client implementation
- Custom audio processing pipeline
- Text messaging is primary
- Browser-based voice calls
- Mobile applications
- Lowest possible latency
- P2P reduces server bandwidth
Next steps
- WebRTC gateway - Browser real-time communication
- SIP trunking - Telephony provider integration
- Voice configuration - Configure AI voice and TTS