Relay SDK for Ruby
Getting Started
The Relay SDK for Ruby enables Ruby developers to connect and use SignalWire's Relay APIs within their own Ruby code. Our Relay SDK allows developers to build or add robust and innovative communication services to their applications.
The Relay SDK for Ruby is easy to use and only takes a few minutes to setup and get running.
Installation
Install the gem using RubyGems:
gem install signalwire
Or add it to your Gemfile
gem "signalwire"
Then require it in your scripts:
require "signalwire"
Minimum Requirements
The Relay SDK gem requires Ruby version 2.0 or higher.
Using the SDK
The Ruby SDK requires your project and token from your SignalWire dashboard. Credentials can be passed in to initializers, or you can set the SIGNALWIRE_PROJECT_KEY
and SIGNALWIRE_TOKEN
environment variables and they will be automatically picked up by the client.
The recommended starting point is the Relay.Consumer
class, and Relay.Client
provides lower-level access.
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.
require "signalwire"
class MyConsumer < Signalwire::Relay::Consumer
contexts ['incoming']
def on_incoming_call(call)
call.answer
call.play_tts 'the quick brown fox jumps over the lazy dog'
call.hangup
end
end
MyConsumer.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.
require 'signalwire/relay/task'
task = Signalwire::Relay::Task.new(project: "your-project-id", token: "your-project-token")
task.deliver(context: 'incoming', message: { number_to_call: '+1555XXXXXXX', message_to_play: 'We have a message for you' })
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.
require "signalwire"
client = Signalwire::Relay::Client.new(project: "your-project-id", token: "your-project-token")
client.on :ready do
call_handle = client.calling.new_call(from: "+1XXXXXXXXXX", to: "+1YYYYYYYYYY")
call_handle.on :answered do
# your call has been answered
end
end
client.connect!
Learn more about Relay Clients
Contexts
Relay uses Contexts as a simple way to separate events to specific consumers, allowing you to write consumers for specific types of calls or messages or scale them independently. A Context is simply a named string, that allows you to categorize requests. When creating outbound requests, or configuring phone numbers for inbound requests, you can specify the context; Relay will then deliver that call or event to Consumers that are configured to listen for that context.
For example, you could have a customer support phone number configured to send to Relay with the support
context, and a personal number configured with personal
context. Relay would deliver these events to any Consumer listening for those contexts. This gives you a lot of control in how messages are delivered to your Consumers, allowing you to write Consumer classes specific to the context, scale them independently, or separate traffic based on your own business rules.