Relay.Consumer
A RELAY Consumer is a simple PHP class 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.
Creating Consumers
A RELAY Consumer is a simple 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.
Coroutine & yield keywords
In the Consumer examples you can see the special keyword yield and the return type Coroutine on the onIncomingCall method. These keywords help you write asynchronous code that “seems” synchronous, avoiding the need to nest your code in multiple callbacks.
Since the Relay PHP SDK is built on top of ReactPHP, a lot of methods return Promises that will be resolved with the result of the asynchronous operations. By flattening out the nested callbacks, you can just yield the Promises to wait for its value.
If you are familiar with Javascript, yield is similar to the async/await syntax in JS.
With yield
Without yield
Important
Within functions with return type Coroutine you must force PHP to parse the function as a Generator so, if you don’t need any yield in your code, just set the first line as:
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.
ready
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.
onTask
Executed with your message sent through a Relay.Task.
onIncomingMessage
Executed when you receive an inbound SMS or MMS, passes in the inbound Message object.
onMessageStateChange
Executed when a message state changes, passes in the inbound Message 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.
Just implement a teardown method in your consumer and it will be called during the shutdown procedure.
Running Consumers
Running a consumer is just like running any other PHP script, simply execute the script as a separate process and ensure you invoke the ->run(); method at the end of your script. 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, so using those systems will make things easier.