RELAY SDK for Python
The RELAY v4 is the most up-to-date version of the Realtime RELAY SDK. Consider upgrading to take advantage of the latest features and improvements.
About RELAY Realtime SDK v2
The SignalWire Realtime SDK v2 provides multi-language support for building real-time communication applications with persistent WebSocket connections.
Capabilities
- Voice (Calling): Make and receive calls, control call flow, play audio, record calls, detect answering machines, and implement complex IVR systems
- Messaging: Send and receive SMS/MMS messages with delivery tracking and media support
- Tasking: Queue and process background jobs with reliable delivery
SDK Components
The SDK provides three components for connecting to RELAY:
- Consumer: Long-running process for handling incoming events. Recommended for most use cases.
- Task: Queue jobs for Consumers from short-lived processes (e.g., web requests). Useful when you can’t maintain a persistent connection.
- Client: Lower-level connection for outbound-only scripts or when you need custom control over the connection.
Contexts
RELAY uses Contexts to route events to specific consumers. A context is a named string that categorizes requests, allowing you to:
- Write consumers for specific types of calls or messages
- Scale different workloads independently
- Separate traffic based on business rules
For example, configure a support phone number with the support context and a personal number with personal context. RELAY delivers events only to consumers listening for those contexts.
Getting Started
The RELAY SDK for Python enables developers to connect and use SignalWire’s RELAY APIs within their own Python code.
Installation
Install the package using either pip or pipenv:
Using pip:
Using pipenv:
Minimum Requirements
The Python SDK is build on top of asyncio and requires at least Python 3.6 to be run.
We suggest to use Python 3.7 or above.
Using the SDK
To use the SDK, you need your project and token from your SignalWire dashboard.
RELAY Consumer
A Relay.Consumer creates a long running process, allowing you to respond to incoming requests and events in realtime. RELAY Consumers abstract all the setup of connecting to RELAY and automatically dispatches workers to handle requests; so you can concentrate on writing your code without having to worry about multi-threading or blocking, everything just works. Think of RELAY Consumers like a background worker system for all your calling and messaging needs.
RELAY Consumers can scale easily, simply by running multiple instances of your Relay.Consumer process. Each event will only be delivered to a single consumer, so as your volume increases, just scale up! This process works well whether you are using Docker Swarm, a Procfile on Heroku, your own webserver, and most other environments.
Setting up a new consumer is the easiest way to get up and running:
Learn more about RELAY Consumers
RELAY Task
A Relay.Task is simple way to send jobs to your Relay.Consumers from a short lived process, like a web framework. RELAY Tasks allow you to pass commands down to your Consumers without blocking your short lived request. Think of a RELAY Task as a way to queue a job for your background workers to processes asynchronously.
For example, if you wanted to make an outbound call and play a message when your user clicks a button on your web application, since RELAY is a realtime protocol and relies on you to tell it what to do in realtime, if you did this within your web application, your web server would block until the call was finished… this may take a long time! Instead, simply create a new RELAY Task. This task will be handled by a running RELAY Consumer process and your web application can respond back to your user immediately.
Send a task in the office context with custom data:
Handle the task in a Consumer:
RELAY Client
Relay.Client is a lower level object, giving you a basic connection to RELAY but that is all. It is best used when you are creating a script only concerned with sending outbound requests or you want complete control over the RELAY connection yourself.
Setting up a new client and make an outbound call: