RELAY SDK for Ruby

View as Markdown

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 Ruby enables Ruby developers to connect and use SignalWire’s RELAY APIs within their own Ruby code.

Installation

Install the gem using RubyGems:

$gem install signalwire

Or add it to your Gemfile

1gem "signalwire"

Then require it in your scripts:

1require "signalwire"

Minimum Requirements

The Relay SDK gem requires Ruby version 2.0 or higher.

Using the SDK

To use the SDK, you need your project and token from your SignalWire dashboard. Credentials can be passed to initializers, or set via SIGNALWIRE_PROJECT_KEY and SIGNALWIRE_TOKEN environment variables.

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.

1require "signalwire"
2
3class MyConsumer < Signalwire::Relay::Consumer
4 contexts ['incoming']
5
6 def on_incoming_call(call)
7 call.answer
8 call.play_tts 'the quick brown fox jumps over the lazy dog'
9 call.hangup
10 end
11end
12
13MyConsumer.new.run

Learn more about RELAY Consumers

RELAY Task

A SignalWire::Relay::Task is a simple way to send jobs to your Relay.Consumer 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.

1require 'signalwire/relay/task'
2
3task = Signalwire::Relay::Task.new(project: "your-project-id", token: "your-project-token")
4task.deliver(context: 'incoming', message: { number_to_call: '+1555XXXXXXX', message_to_play: 'We have a message for you' })

Learn more about RELAY Tasks

RELAY Client

Relay.Client is the underlying Relay connection Consumers use. It offers an alternative API for cases that require more specialized functionality.

Setting up a new client and make an outbound call.

1require "signalwire"
2
3client = Signalwire::Relay::Client.new(project: "your-project-id", token: "your-project-token")
4
5client.on :ready do
6 call_handle = client.calling.new_call(from: "+1XXXXXXXXXX", to: "+1YYYYYYYYYY")
7 call_handle.on :answered do
8 # your call has been answered
9 end
10end
11
12client.connect!

Learn more about RELAY Clients