Skip to main content

Overview

Iqra AI provides a WebRTC gateway for embedding AI voice conversations directly in web browsers and mobile applications. WebRTC enables low-latency, peer-to-peer audio communication without requiring plugins or downloads. The WebRTC implementation uses:
  • SIPSorcery library for WebRTC peer connection management
  • WebSocket signaling for SDP/ICE exchange
  • STUN servers for NAT traversal
  • Audio transceivers for bidirectional media

Architecture

Connection flow

Dual-transport design

The WebRtcClientTransport combines:
  1. WebSocket channel - For signaling (SDP, ICE) and text messages
  2. RTP channel - For audio media via WebRTC peer connection
Implementation: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:13

Session initialization

1

Create web session

Client requests a WebRTC session via API:
2

Backend prepares session

Backend creates:
  • Conversation session orchestrator
  • AI agent instance
  • Deferred client transport (waiting for WebSocket)
  • WebSocket URL with authentication token
Source: IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:111
3

Connect WebSocket

Client connects to the provided WebSocket URL:
Backend validates the session token and activates the transport:
Source: IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:280
4

WebRTC negotiation

Client creates peer connection and sends offer to backend via WebSocket.

WebRTC peer connection

Client-side setup

Backend implementation

The backend uses SIPSorcery to handle WebRTC:
Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:38

Audio codec configuration

Supported codecs

The backend dynamically selects codecs based on session configuration:
Codec mapping: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:72

Audio configuration object

Signaling protocol

Message types

WebSocket messages during WebRTC setup:

Offer (Client → Backend)

Answer (Backend → Client)

ICE Candidate (Bidirectional)

Signaling loop

Backend maintains signaling channel:
Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:84

Data channel support

WebRTC includes a data channel for text messaging:

Backend setup

Client usage

Connection states

Monitoring connection health

Backend monitoring

Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:239

Media streaming

Outbound audio (Backend → Client)

Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:183

Inbound audio (Client → Backend)

Source: IqraInfrastructure/Managers/Conversation/Session/Client/Transport/WebRtcClientTransport.cs:174

Mobile implementation

React Native example

iOS/Swift with WebRTC SDK

Security considerations

Token validation: Backend validates session tokens before activating WebRTC transport to prevent unauthorized access.
Source: IqraInfrastructure/Managers/WebSession/BackendWebSessionProcessorManager.cs:250
STUN vs TURN: Current implementation uses STUN for NAT traversal. For production, consider adding TURN servers for users behind restrictive firewalls.

Troubleshooting

ICE connection failures

Symptom: Peer connection stuck in “checking” state Solutions:
  • Verify STUN server is reachable
  • Check firewall rules allow UDP traffic
  • Consider deploying TURN servers for relaying
  • Enable verbose ICE logging

Audio quality issues

Symptom: Choppy or distorted audio Solutions:
  • Verify codec compatibility (prefer OPUS)
  • Check network bandwidth
  • Monitor packet loss via WebRTC stats
  • Adjust frame duration (20ms recommended)

SDP negotiation failures

Symptom: setRemoteDescription fails Common causes:
  • Codec mismatch (backend doesn’t support offered codec)
  • Invalid SDP format
  • Missing required media sections
Debug: Log full SDP exchange

Performance optimization

Use Opus with FEC: Enable forward error correction to handle packet loss without retransmissions.
Optimize frame size: 20ms frames balance latency and packet overhead. Smaller frames = lower latency but more overhead.
Monitor RTP stats: Track jitter, packet loss, and round-trip time to detect quality degradation early.

Next steps