RELAY Client
What Is RELAY?
RELAY is SignalWire’s real-time WebSocket protocol for programmatic call control. While the agent-based approach (AgentBase + SWML) lets SignalWire’s AI handle conversations declaratively, RELAY gives you imperative, event-driven control over every aspect of a call.
When to Use RELAY vs Agents
Use RELAY when you need fine-grained control over call flow — answering, playing prompts, collecting digits, recording, bridging, conferencing — without an AI agent in the loop.
Installation
The RELAY client is included in the signalwire package:
It requires the websockets library (installed automatically as a dependency).
Quick Start
Python
TypeScript
relay_hello.py
Authentication
The RELAY client supports two authentication methods:
Project + API Token
JWT Token
Environment Variables
All credentials can be provided via environment variables instead of constructor arguments:
Connection Lifecycle
The RELAY client manages a persistent WebSocket connection with automatic reconnection:
- Connect — Establishes WebSocket to
wss://<host> - Authenticate — Sends
signalwire.connectwith credentials - Subscribe — Registers for events on specified contexts
- Event Loop — Processes events until disconnected
- Reconnect — Automatic reconnection with exponential backoff (1s to 30s max)
Contexts
Contexts determine which inbound calls your client receives. Pass them in the constructor or subscribe dynamically:
Async Context Manager
Handling Inbound Calls
Register a handler with the @client.on_call decorator:
relay_inbound.py
The Call object provides:
Making Outbound Calls
Use client.dial() to initiate outbound calls:
relay_outbound.py
The devices parameter supports serial and parallel dialing:
Call Control Methods
Key call control methods across languages:
Audio Playback
Recording
Input Collection
Detection (Answering Machine, Fax, DTMF)
Bridging / Connecting
Conference
Hold / Unhold
Noise Reduction
AI Agent on a Call
You can start an AI agent session on a RELAY-controlled call:
The Action Pattern
Most call control methods return an Action object — an async handle for the ongoing operation:
Common Action Methods
on_completed Callback
Every action-based method accepts an on_completed callback:
SMS/MMS Messaging
The RELAY client supports sending and receiving SMS/MMS messages.
Sending Messages
Sending MMS (with media)
Receiving Messages
The Message object provides:
Event Listeners
Register per-call event listeners for fine-grained control:
Advanced Configuration
Max Active Calls
Limit concurrent calls to prevent resource exhaustion:
Or via environment variable:
Default: 1000.
Connection Limits
By default, only one RelayClient connection is allowed per process:
Error Handling
The client handles errors gracefully — server errors from call methods return empty dicts rather than raising exceptions. Connection-level errors trigger automatic reconnection.
For explicit error handling:
Complete Example: IVR System
relay_ivr.py