Skip to main content

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
Unlike WebRTC, WebSocket uses a client-server model with direct binary audio streaming over a persistent TCP connection.

Architecture

Connection model

Key difference from WebRTC: WebSocket is a simple bidirectional channel without SDP negotiation, ICE candidates, or RTP encapsulation.

Transport implementation

The WebSocketClientTransport handles both text and binary messages:
  • Binary messages: Raw audio frames (PCM, μ-law, A-law, etc.)
  • Text messages: Control signals, metadata, transcripts
Implementation: 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:271
4

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:
Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:32

Sending audio from backend

Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:85

Audio format configuration

Session configuration

Specify audio encoding in the session request:

Supported codecs

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

Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:95

Connection lifecycle

Connection establishment

Graceful disconnection

Client-initiated disconnect:
Backend-initiated disconnect:
Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:106

Handling disconnects

Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebSocketClientTransport.cs:45

Security and authentication

Token-based authentication

WebSocket URL includes an HMAC token:
Backend validates token before accepting connection:
Source: IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:250

Token generation

Source: IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:209
Token expiry: Tokens are valid for 5 minutes by default. Client must connect within this window.
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:
  1. Verify binaryType is set to 'arraybuffer'
  2. Check audio format matches session configuration
  3. Confirm onmessage handler processes binary messages
  4. 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
Mitigation:

Comparison: WebSocket vs WebRTC

When to choose WebSocket:
  • Backend service integration
  • Simpler client implementation
  • Custom audio processing pipeline
  • Text messaging is primary
When to choose WebRTC:
  • Browser-based voice calls
  • Mobile applications
  • Lowest possible latency
  • P2P reduces server bandwidth

Next steps