Relay.Consumer

View as Markdown

Relay.Consumer

A RELAY Consumer is a Go object that runs Go routines in the background 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.

Creating Consumers

A RELAY Consumer is an object, customized by specifying contexts and event handlers to respond to incoming events.

A consumer has 2 required properties: project, token, and usually requires at least one contexts for incoming events. Project and Token are used to authenticate your Consumer to your SignalWire account. Contexts are a list of contexts you want this Consumer to listen for. Learn more about RELAY Contexts.

1Contexts = append(Contexts, PContext)
2consumer := new(signalwire.Consumer)
3// setup the Client
4consumer.Setup(ProjectID, PTokenID, Contexts)
5// register callback
6 consumer.OnIncomingCall = MyOnIncomingCall
7 log.Info("Wait incoming call..")
8// start
9 if err := consumer.Run(); err != nil {
10 log.Errorf("Error occurred while starting Signalwire Consumer")
11}

Initializing Consumers

You can optionally add an 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.

Properties

PropertyTypeDescription
clientRelay.ClientThe underlying Relay client object.

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.

onIncomingMessage

Executed when you receive an inbound message, passes in the inbound Message object.

1func MyOnIncomingMessage(consumer *signalwire.Consumer, message *signalwire.MessageObj) {
2 // Handle incoming message
3}
4
5consumer.OnIncomingMessage = MyOnIncomingMessage

onMessageStateChange

Executed when the state of a message changes.

1func MyOnMessageStateChange(consumer *signalwire.Consumer, message *signalwire.MessageObj) {
2 // Handle message state change
3}
4
5consumer.OnMessageStateChange = MyOnMessageStateChange

onTask

Executed when a task is delivered to your Consumer.

1func MyOnTask(consumer *signalwire.Consumer, task signalwire.ParamsEventTaskingTask) {
2 // Handle task
3}
4
5consumer.OnTask = MyOnTask

onReady

Executed once your Consumer is connected to RELAY and the session has been established.

1func MyReady(consumer *signalwire.Consumer) {
2 resultDial := consumer.Client.Calling.DialPhone(fromNumber, toNumber)
3 if !resultDial.Successful {
4 if err := consumer.Stop(); err != nil {
5 log.Errorf("Error occurred while trying to stop Consumer")
6 }
7
8 return
9 }
10}
11
12func main() {
13 consumer := new(signalwire.Consumer)
14 // setup the Client
15 consumer.Setup(PProjectID, PTokenID, Contexts)
16 // register callback
17 consumer.Ready = MyReady
18 // start
19 if err := consumer.Run(); err != nil {
20 log.Errorf("Error occurred while starting Signalwire Consumer")
21 }
22}

onIncomingCall

Executed when you receive an inbound call, passes in the inbound Call object.

1// MyOnIncomingCall - gets executed when we receive an incoming call
2func MyOnIncomingCall(consumer *signalwire.Consumer, call *signalwire.CallObj) {
3 resultAnswer := call.Answer()
4 if !resultAnswer.Successful {
5 if err := consumer.Stop(); err != nil {
6 log.Errorf("Error occurred while trying to stop Consumer")
7 }
8
9 return
10 }
11
12 log.Info("Playing audio on call..")
13
14 if _, err := call.PlayAudio("https://cdn.signalwire.com/default-music/welcome.mp3"); err != nil {
15 log.Errorf("Error occurred while trying to play audio")
16 }
17 if err := call.Hangup(); err != nil {
18 log.Errorf("Error occurred while trying to hangup call")
19 }
20
21 if err := consumer.Stop(); err != nil {
22 log.Errorf("Error occurred while trying to stop Consumer")
23 }
24}

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.

Implement an Teardown method in your consumer and it will be called during the shutdown procedure.

1func MyTeardown(consumer *signalwire.Consumer) {
2 file.Close()
3}
4consumer.Teardown = MyTeardown

Running Consumers

1if err := consumer.Run(); err != nil {
2 og.Errorf("Error occurred while starting Signalwire Consumer")
3}

Shutting Down Consumers

1if err := consumer.Stop(); err != nil {
2 log.Errorf("Error occurred while trying to stop Consumer")
3}