chan_websocket) — enabling real-time bidirectional audio streaming for AI-powered voice conversations.
Both transports follow the same two-phase flow: an HTTP webhook registers the call, then a media connection (TCP or WebSocket) carries the audio. Choose the transport that fits your Asterisk version and network topology.
Transport Comparison
| Feature | AudioSocket (Native) | WebSocket |
|---|---|---|
| Asterisk Module | res_audiosocket | chan_websocket |
| Protocol | Raw TCP | WebSocket (wss://) |
| Port | 4573 | 443 (standard HTTPS) |
| Audio Codec | SLIN 16-bit 8kHz | μ-law 8kHz |
| Firewall | Requires TCP 4573 open | Uses standard HTTPS port |
| TLS | Not built-in | Native TLS via wss:// |
| Asterisk Version | 16+ | 20+ |
| Best For | LAN / private network | Cloud / NAT traversal |
Architecture Overview
Both transports share the same backend pipeline. The only difference is the Streamer layer that bridges the media connection to the AI pipeline.Two-Phase Call Setup
Every Asterisk call (inbound or outbound, AudioSocket or WebSocket) follows this pattern:- Phase 1 — Webhook: An HTTP request registers the call and gets a
contextId - Phase 2 — Media: A transport connection (TCP or WebSocket) streams audio using that
contextId
Prerequisites
Asterisk Server
Asterisk 16+ (AudioSocket) or 20+ (WebSocket) with the required module loaded
Rapida Account
Active Rapida account with an API key (
rpd-xxx)Voice Assistant
A configured Rapida voice assistant with phone deployment enabled
Network Access
TCP 4573 (AudioSocket) or HTTPS 443 (WebSocket) to Rapida
For outbound calls, you also need Asterisk ARI enabled with credentials stored in a Rapida vault credential.
Quick Start — Inbound Calls
Choose your transport and follow the matching setup:Inbound Call Flow — Detailed
Sequence Diagram
What Happens at Each Step
| Step | Component | Action |
|---|---|---|
| 1 | Asterisk dialplan | Answers call, sets SLIN audio format |
| 2 | CURL() function | HTTP GET to Rapida webhook — creates conversation, saves CallContext to Redis |
| 3 | Webhook response | Returns contextId as plain text (UUID) |
| 4a | AudioSocket() app | Opens TCP to Rapida:4573, sends contextId as UUID frame |
| 4b | WebSocket() app | Opens WSS to wss://host/v1/talk/asterisk/ctx/{contextId} |
| 5 | Rapida transport | Atomically reads and deletes CallContext from Redis (prevents replay) |
| 6 | AI pipeline | STT → LLM → TTS loop until hangup |
Outbound Call Flow — Detailed
Outbound calls are initiated via the Rapida SDK (gRPCCreatePhoneCall RPC) or REST API. Rapida uses Asterisk’s ARI (Asterisk REST Interface) to originate the call.
Prerequisites for Outbound
Create Vault Credential in Rapida
In the Rapida dashboard, go to Credentials → Create Credential and select Asterisk. Provide:
| Field | Required | Example | Description |
|---|---|---|---|
ari_url | Yes | http://asterisk.local:8088 | Base URL of Asterisk ARI |
ari_user | Yes | rapida | ARI username |
ari_password | Yes | your-ari-password | ARI password |
endpoint_technology | No | PJSIP | SIP technology (default: PJSIP) |
trunk | No | my-sip-trunk | SIP trunk name for routing |
Configure Outbound Dialplan
ARI can originate calls in two modes. Choose one:
- Stasis App Mode (Recommended)
- Dialplan Mode
ARI originates the channel directly into a Stasis application — no dialplan needed for the outbound leg. The channel is handled programmatically.Rapida sends:
POST /ari/channels?endpoint=PJSIP/+15551234567&app=rapidaAssign Credential to Phone Deployment
In the assistant’s Phone Deployment, select the Asterisk vault credential and configure:
| Option | Value | Description |
|---|---|---|
| Telephony Provider | asterisk | Selects Asterisk integration |
| Credential | (your vault credential) | ARI connection details |
| Phone Number | +15559876543 | Caller ID for outbound |
| Context | (optional) | Dialplan context for outbound |
| Extension | (optional) | Dialplan extension (default: s) |
| App | (optional) | Stasis app name (default: rapida) |
Outbound Sequence Diagram
SDK Code Examples (Outbound)
- Python
- Node.js
- Go
- cURL
AudioSocket Protocol Reference
Frame Format
Each frame has a 3-byte header followed by a variable-length payload:Frame Types
| Type | Hex | Direction | Description |
|---|---|---|---|
| Hangup | 0x00 | Both | Call termination |
| UUID | 0x01 | Asterisk → Rapida | Context ID (sent as first frame) |
| Silence | 0x02 | Asterisk → Rapida | Silence indicator |
| Audio | 0x10 | Both | Audio data (SLIN 16-bit) |
| Error | 0xFF | Both | Error condition |
Audio Format
| Property | AudioSocket (SLIN) | WebSocket (μ-law) | Rapida Internal |
|---|---|---|---|
| Encoding | Signed Linear 16-bit | μ-law 8-bit | Linear PCM 16-bit |
| Sample Rate | 8000 Hz | 8000 Hz | 16000 Hz |
| Channels | Mono | Mono | Mono |
Status Callbacks
Rapida sends status updates to Asterisk via ARI events. You can also configure a webhook on the assistant to receive call lifecycle events.Event Callback URL
Event Types
| Event | Trigger |
|---|---|
webhook | Initial call registration |
channel_created | ARI channel created (outbound) |
call.connected | Media connection established |
call.ended | Call terminated normally |
call.failed | Error during call |
Credential Configuration
Vault Credential Fields
When creating an Asterisk credential in the Rapida vault, provide:| Field | Required | Default | Description |
|---|---|---|---|
ari_url | Yes (outbound only) | — | Base URL of Asterisk ARI HTTP server |
ari_user | Yes (outbound only) | — | ARI username |
ari_password | Yes (outbound only) | — | ARI password |
endpoint_technology | No | PJSIP | Channel technology: PJSIP, SIP, IAX2, DAHDI |
trunk | No | — | SIP trunk for routing: builds PJSIP/trunk/number |
Phone Deployment Options
These options are configured on the assistant’s phone deployment:| Option | Default | Description |
|---|---|---|
phone | — | Default caller ID for outbound calls |
context | (none) | Asterisk dialplan context for outbound |
extension | s | Dialplan extension |
app | rapida | Stasis application name |
endpoint_technology | PJSIP | Overrides vault credential |
trunk | — | Overrides vault credential |
Full Configuration Examples
Example 1: Inbound Only (AudioSocket)
Simplest setup — no ARI needed, no vault credential required for inbound-only.- Asterisk Config
- Network Requirements
Example 2: Inbound Only (WebSocket)
No firewall port needed beyond HTTPS — ideal for cloud-hosted Asterisk.Example 3: Inbound + Outbound (Full Setup)
Complete setup with ARI for outbound calls and AudioSocket for media.- extensions.conf
- ari.conf
- http.conf
- Rapida Vault Credential
- Deployment Options
- Network Requirements
Example 4: WebSocket-Only (Inbound + Outbound)
Zero custom ports — everything runs over HTTPS.- extensions.conf
- Rapida Deployment Options
Call Lifecycle
State Machine
Captured Metrics
Each call automatically records:| Metric | Description |
|---|---|
call.duration | Total call time (seconds) |
call.talk_duration | Active speech time |
call.status | completed, failed, no_answer |
call.disconnected_by | user, assistant, system |
Troubleshooting
Connection Refused (AudioSocket)
Connection Refused (AudioSocket)
Symptoms:
AudioSocket() returns immediately, no audioChecklist:- Verify Rapida host is reachable:
nc -zv api.rapida.ai 4573 - Check firewall allows TCP 4573 outbound
- Ensure
res_audiosocket.sois loaded - Verify the contextId (token) is non-empty in the dialplan
WebSocket Connection Failed
WebSocket Connection Failed
Symptoms:
WebSocket() fails, no audioChecklist:- Verify WSS URL is correct:
wss://api.rapida.ai/v1/talk/asterisk/ctx/{contextId} - Ensure
chan_websocket.sois loaded - Check TLS certificate trust on the Asterisk server
- Verify Asterisk version is 20+ for
chan_websocket
Call Context Not Found
Call Context Not Found
Symptoms: Transport connects but immediately drops with “call context not found”Checklist:
- Context ID expires after 5 minutes — ensure fast connection after webhook
- Each contextId is single-use (atomically deleted on first connection)
- Don’t reuse contextIds across calls
- Verify the webhook returned a valid contextId (not an error message)
No Audio / One-Way Audio (AudioSocket)
No Audio / One-Way Audio (AudioSocket)
Symptoms: AI speaks but doesn’t hear, or vice versaChecklist:
- Set
CHANNEL(audioreadformat)=slinandCHANNEL(audiowriteformat)=slin - Verify
res_audiosocketmodule is loaded - Check Asterisk timing:
asterisk -rx "timing test"
Choppy Audio / Delays
Choppy Audio / Delays
Symptoms: Audio artifacts, long pausesChecklist:
- Check network latency to Rapida:
ping api.rapida.ai(target < 100ms) - Verify no packet loss on the path
- For WebSocket: ensure no proxy is buffering WebSocket frames
- Reduce network hops if possible
ARI Outbound Call Fails
ARI Outbound Call Fails
Symptoms: SDK call returns error, no ring on customer phoneChecklist:
- Verify ARI is enabled:
asterisk -rx "http show status" - Check ARI credentials in vault:
ari_url,ari_user,ari_password - Verify endpoint format:
PJSIP/trunk/numberorPJSIP/number - Check ARI URL is reachable from Rapida: must not be behind NAT without port forwarding
- Review Asterisk CLI:
asterisk -rvvvfor ARI request logs
RAPIDA_CONTEXT_ID Empty in Outbound Dialplan
RAPIDA_CONTEXT_ID Empty in Outbound Dialplan
Symptoms: Outbound call connects but AudioSocket/WebSocket has no contextChecklist:
- Verify ARI channel variables are configured:
{"variables": {"RAPIDA_CONTEXT_ID": "..."}} - Check the dialplan references
${RAPIDA_CONTEXT_ID}(exact case) - Verify deployment options include
contextandextension
Debug Commands
High Availability
Multi-Node Deployment
Dialplan with failover (AudioSocket):API Reference
Inbound Webhook
| Parameter | Location | Required | Description |
|---|---|---|---|
assistantId | Path | Yes | Rapida assistant ID |
from | Query | Yes | Caller phone number |
x-api-key | Query or Header | Yes | API key for authentication |
channel_id | Query | No | Asterisk channel ID |
contextId (UUID)
WebSocket Media Endpoint
| Parameter | Location | Required | Description |
|---|---|---|---|
contextId | Path | Yes | Context ID from webhook response |
Status Callback
type field.