Event Listener

Event listeners constantly listen for messages with certain pre-defined routing keys to be dispatched wither via RabbitMQ or the local message broker, and upon receiving a message, perform any necessary action. These are used for several different reasons:

  • Provide extensibility allowing other packages to trigger code upon a certain event occuring (eg. new user registers).
  • Queue certain actions to be syphoned of and processed by another server / system. Usually reserved for resource intensive operations, but not always.
  • Help increase security by having RabbitMQ on a separate server hidden behind private networking and heavily locked down, allowing secure operations to be processed in a more secure environment.

You may create a new event listener by using the create listener CLI command, such as:

apex create listener <PACKAGE> <ALIAS> [--routing-key <ROUTING_KEY>]

apex create listener my-shop shop-user-listener --routing-key users.profile

Using the above example, a new file will be created at /src/MyShop/Listeners/ShopUserListener.php, which only contains one example method. This class also contains one property named $routing_key which is the first two segments of the routing key that the class accepts messages for, while the third argument of the routing key is the method name within this class.

For full information regarding event listners and how they are developed, please visit the Event Listeners and Queues section of the documentation.