Relay.Consumer
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.
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
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.
onMessageStateChange
Executed when the state of a message changes.
onTask
Executed when a task is delivered to your Consumer.
onReady
Executed once your Consumer is connected to RELAY and the session has been established.
onIncomingCall
Executed when you receive an inbound call, passes in the inbound Call object.
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.