Relay Consumer
A Relay Consumer is a simple object that runs in its own process along side your application to handle calling and messaging events in realtime. Relay Consumers abstract all the setup of connecting to Relay and automatically dispatch workers to handle requests. Consumers will receive requests and delegate them to their own worker thread, allowing you to focus on your business logic 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.
Authenticating a consumer
Authentication requires a SignalWire project ID and a token. You can generate one from your dashboard.
The values can be passed in either as the project
and token
parameters to the constructor, or by setting the SIGNALWIRE_PROJECT_KEY
and SIGNALWIRE_TOKEN
environment variables.
An example using constructor parameters:
class MyConsumer < Signalwire::Relay::Consumer
contexts ['incoming']
def on_incoming_call(call)
call.answer
call.play_tts 'this consumer uses constructor parameters'
call.hangup
end
end
MyConsumer.new(project: 'your-project-key', token: 'your-project-token').run
Consumer Contexts
A Relay Consumer is a simple object, customized by specifying contexts and event handlers to respond to incoming events.
A consumer usually requires at least one contexts
for incoming events. Contexts are a list of contexts you want this Consumer to listen for. Learn more about Contexts.
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
Initializing Consumers
You can optionally define a setup
method if you need to do any initialization work before processing messages. This is useful to do any one-off work that you wouldn't want to do for each and every event, such as setting up logging or connecting to a datastore.
class MyConsumer < Signalwire::Relay::Consumer
contexts ['incoming']
def setup
SomeDatabase.connect
end
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
Event Handlers
Event handlers are where you will write most of your code. They are executed when your consumer receives a matching event for the contexts specified by your Consumer.
on_incoming_call
Executed when you receive an inbound call, passes in the inbound Call
object.
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
ready
This method is executed when the Consumer has connected and is ready to make Relay requests.
require 'signalwire'
class MyConsumer < Signalwire::Relay::Consumer
def ready
logger.debug "The consumer is ready to execute actions now"
# Send an SMS on ready
result = client.messaging.send(from: "+1XXXXXXXXXX", to: "+1YYYYYYYYYY", context: 'incoming', body: 'Hello from SignalWire!')
logger.debug "message id #{result.message_id} was successfully sent" if result.successful
end
end
MyConsumer.new.run
on_task
Receives your message sent through a Relay::Task
.
require 'signalwire'
class MyConsumer < Signalwire::Relay::Consumer
contexts ['incoming']
def on_task(task)
logger.debug "Received #{task.message}"
end
end
MyConsumer.new.run
on_incoming_message
This method is executed when the consumer receives an inbound text message on one of the subscribed contexts. Receives a Message
object as a parameter.
class MessageReceiveConsumer < Signalwire::Relay::Consumer
contexts ['office']
def on_incoming_message(message)
logger.info "Received message from #{message.from}: #{message.body}"
end
end
MessageReceiveConsumer.new.run
on_message_state_change
Executed when a message state changes in a context the consumer is subscribed to. Receives a Message
object as a parameter.
class MessageSendConsumer < Signalwire::Relay::Consumer
def on_message_state_change(message)
logger.debug "message id #{message.id} now has state #{message.state}"
end
end
MessageSendConsumer.new.run
Cleaning Up on Exit
When a Relay Consumer shuts down, you have the opportunity to clean up any resources held by the consumer. For example, you could close any open files, network connections, or send a notification to your monitoring service.
Just implement a teardown
method in your consumer and it will be called during the shutdown procedure.
class MyConsumer < Signalwire::Relay::Consumer
contexts ['incoming']
def teardown
SomeDatabase.disconnect
end
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
Running Consumers
Running a consumer is just like running any Ruby script, simply execute the script as a separate process and call run
to start it. The process will stay up until you shut it down.
Shutting Down Consumers
In order to gracefully shut down a Relay consumer process, send it the SIGTERM
signal. Most process supervisors such as Runit, Docker and Kubernetes send this signal when shutting down a process.