What RabbitMQ Can Do For You
James TitcumbNomad PHP Lightning Talk
May 2014
Who is this guy?
James Titcumb
www.jamestitcumb.com
www.protected.co.uk
www.phphants.co.uk
@asgrim
What is message queuing?
Separate applications
Low cost parallelisation
Basic Example
Publisher
queue
Consumer
Based on: http://www.rabbitmq.com/tutorials/tutorial-one-php.html
Exchanges
Based on: http://www.rabbitmq.com/tutorials/tutorial-three-php.html
queue1
Publisher
Publisher
Publisher
Publisher
Exchange
queue2
Consumer
Consumer
Consumer
Exchanges: Direct
Based on: http://www.rabbitmq.com/tutorials/tutorial-four-php.html
fruit
Exchange
vegetable
Fruit consumer
Veg consumer
Fruit consumerPublisher
routing key: fruit
routing key: vegetable
Exchanges: Topic
Based on: http://www.rabbitmq.com/tutorials/tutorial-five-php.html
*.fruit
Exchange
green.*
Fruit consumer
Green consumer
Veg consumerPublisher
routing key: green.fruit
routing key: green.vegetable
*.vegetable
Real World Uses
● Fast logging solution● Sending emails● Sending SMS● Background processing (e.g. analysing data)
Using in PHP?https://github.com/videlalvaro/php-amqplib
composer.json
{
"require": {
"videlalvaro/php-amqplib": "2.*"
}
}
Set up the channel
use PhpAmqpLib\Connection\AMQPConnection;
$connection = new AMQPConnection(
'localhost', 5672, 'guest', 'guest', '/');
$channel = $connection->channel();
$channel->queue_declare('my_queue', /* params... */);
$channel->exchange_declare(
'my_exchange', 'fanout', /* params... */);
$channel->queue_bind('my_queue', 'my_exchange');
Basic Publishing Example
use PhpAmqpLib\Message\AMQPMessage;
$content = 'Hello world';
$message = new AMQPMessage($content);
$channel->basic_publish($message);
Basic Consumer Example
use PhpAmqpLib\Message\AMQPMessage;
$channel->basic_consume(
'my_queue',
/* some more parameters */
function (AMQPMessage $message) {
echo $message->body;
}
);
Questions?
Thank you!
James Titcumb
www.jamestitcumb.com
www.protected.co.uk
www.phphants.co.uk
@asgrim
Please feedback!
https://joind.in/11350