PHP code example of so-php / pubsub

1. Go to this page and download the library: Download so-php/pubsub library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

so-php / pubsub example snippets


// need a channel to work with
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();

$pubSub = new PubSub($ch, EXCHANGE_NAME);
$pubSub->publish('foo', array('hello'=>'world'));

// need a channel to work with
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();

function doSomethingOnEvent(Event e){
   echo "Received event " . $e->getName() . " which was triggered on " . $e->getDateTime()->format('Y-m-d H:i:s') . " with the following params: " . print_r($e->getParams(),true);
}

$pubSub = new PubSub($ch, EXCHANGE_NAME);
$pubSub->subscribe('foo', 'doSomethingOnEvent');

// then wait for (and handle events)
// either:
$pubSub->wait(); // wraps $ch->wait() internally
// or directly on the channel object
$ch->wait();