Skip to content

Instantly share code, notes, and snippets.

@sokil
Created August 26, 2014 13:26
Show Gist options
  • Save sokil/1e3dd7b4effa6929e2ae to your computer and use it in GitHub Desktop.
Save sokil/1e3dd7b4effa6929e2ae to your computer and use it in GitHub Desktop.
RabbitMQ Publisher-Subscriber (https://github.com/videlalvaro/php-amqplib)
<?php
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
// connect
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
// create channel
$channel = $connection->channel();
// declare exchange
$channel->exchange_declare('ExchangeName', 'topic', false, false, false);
// send message
$message = new AMQPMessage('say hello at ' . date('Y-m-d H:i:s'), array(
'delivery_mode' => 2,
));
$channel->basic_publish($message, 'ExchangeName', 'errors.warn');
// close
$channel->close();
$connection->close();
<?php
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
// connect
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
// create channel
$channel = $connection->channel();
// declare exchange
$channel->exchange_declare('ExchangeName', 'topic', false, false, false);
list($queueName,,) = $channel->queue_declare('', false, true);
echo 'Queue Name: ' . $queueName . PHP_EOL;
$channel->queue_bind($queueName, 'ExchangeName', '*.warn');
// redister callback
$channel->basic_consume($queueName, '', false, false, true, false, function(AMQPMessage $msg) {
// print message
echo 'Received: ' . $msg->body . PHP_EOL;
});
// start loop
echo 'Start loop' . PHP_EOL;
while(count($channel->callbacks)) {
$channel->wait();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment