What RabbitMQ Can Do For You (Nomad PHP May 2014)

17

Click here to load reader

description

RabbitMQ is a message broker – an application that allows communication between applications by way of a message queuing system. In this talk, we look at some of the basic concepts of RabbitMQ and how it can help effectively scale your applications. These slides are for the lightning talk I gave at Nomad PHP on 22nd May 2014.

Transcript of What RabbitMQ Can Do For You (Nomad PHP May 2014)

Page 1: What RabbitMQ Can Do For You (Nomad PHP May 2014)

What RabbitMQ Can Do For You

James TitcumbNomad PHP Lightning Talk

May 2014

Page 2: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Who is this guy?

James Titcumb

www.jamestitcumb.com

www.protected.co.uk

www.phphants.co.uk

@asgrim

Page 3: What RabbitMQ Can Do For You (Nomad PHP May 2014)

What is message queuing?

Page 4: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Separate applications

Page 5: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Low cost parallelisation

Page 6: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Basic Example

Publisher

queue

Consumer

Based on: http://www.rabbitmq.com/tutorials/tutorial-one-php.html

Page 7: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Exchanges

Based on: http://www.rabbitmq.com/tutorials/tutorial-three-php.html

queue1

Publisher

Publisher

Publisher

Publisher

Exchange

queue2

Consumer

Consumer

Consumer

Page 8: What RabbitMQ Can Do For You (Nomad PHP May 2014)

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

Page 9: What RabbitMQ Can Do For You (Nomad PHP May 2014)

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

Page 10: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Real World Uses

● Fast logging solution● Sending emails● Sending SMS● Background processing (e.g. analysing data)

Page 11: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Using in PHP?https://github.com/videlalvaro/php-amqplib

Page 12: What RabbitMQ Can Do For You (Nomad PHP May 2014)

composer.json

{

"require": {

"videlalvaro/php-amqplib": "2.*"

}

}

Page 13: What RabbitMQ Can Do For You (Nomad PHP May 2014)

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');

Page 14: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Basic Publishing Example

use PhpAmqpLib\Message\AMQPMessage;

$content = 'Hello world';

$message = new AMQPMessage($content);

$channel->basic_publish($message);

Page 15: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Basic Consumer Example

use PhpAmqpLib\Message\AMQPMessage;

$channel->basic_consume(

'my_queue',

/* some more parameters */

function (AMQPMessage $message) {

echo $message->body;

}

);

Page 16: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Questions?

Page 17: What RabbitMQ Can Do For You (Nomad PHP May 2014)

Thank you!

James Titcumb

www.jamestitcumb.com

www.protected.co.uk

www.phphants.co.uk

@asgrim

Please feedback!

https://joind.in/11350