Sending Messages - OggettoWeb/messenger GitHub Wiki

Each outgoing message from Magento should be formed as event object. Each event has name and data.
For example, you want to notify other systems about new order:

<?php
$event = Mage::getModel('messenger/event')
   ->setName('order_created')
   ->setData(['products' => [1, 2, 3], 'subtotal' => 42])

To publish this event you need to define a route first. Each route says which queue message should be pushed to. All the routes are defined in module's config.xml file. For example, you want to publish order_created message to queue named magento_order_updates.

<global>
   <messenger>
      <publish_router>
         <new_order>
            <criterion>
                <name>order_created</name>
            </criterion>
            <queue>magento_order_updates</queue>
         </new_order>
      </publish_router>
   </messenger>
</global>

Here we say, that messages which are matched by certain criterion (name = "order_created") should be routed to magento_order_updates queue.

When route and event are ready, publishing should be done this way:

<?php
$event = ... // see above
Mage::getSingleton('messenger/di')
   ->newInstance('messenger/event_dispatcher')
   ->dispatch($event);

It's also possible to assign observers which will listen to beforeDispatch and afterDispatch events during messages sending.

<?php
$observer = ... // instance of Oggetto_Messenger_Model_Event_DispatchObserver_Interface
Mage::getSingleton('messenger/di')
   ->newInstance('messenger/event_dispatcher')
   ->assignObserver($observer)

Outgoing messages will be published to the message queue as XML. Event from the example above would look like the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<update>
 <event name="order_created">
  <products>1</products>
  <products>2</products>
  <products>3</products>
  <subtotal>42</subtotal>
 </event>
</update>

Messages format could be changed for your own.

Note, that if you are using composer installation, you should require vendor/autoload.php to send messages (you may do it for the whole project).

⚠️ **GitHub.com Fallback** ⚠️