KEMBAR78
RabbitMQ in PHP | PDF
RabbitMQ in PHP
use message broker rabbitmq in php
        Sergio Sicari <sergiosicari@gmail.com>
What is a RabbitMQ?
RabbitMQ is a messaging broker, a service
platform to send and receive messages
implementing the Advanced Message
Queueing Protocol (AMQP).

It's write in Erlang and open source.

It's official supported on a number of operating
system and language (through API)
Install the service
On Ubuntu from repository:

1) Add the following line to your /etc/apt/sources.list:
   deb http://www.rabbitmq.com/debian/ testing main
2) $ apt-get update
3) $ sudo apt-get install rabbitmq-server


You managing the service with 'rabbitmqctl'
  $ rabbitmqctl stop/start
  $ rabbitmqctl status
Managment (rabbitmqadmin*)
The rabbitmq-management plugin provides an HTTP-based
API for management and monitoring of your RabbitMQ
server, along with a browser-based UI and a command line
tool.

You can install with follow command:
$ rabbitmq-plugins enable rabbitmq_management

Now you can manage RabbitMQ from web gui at:
http://localhost:55672 **

* rabbitmqadmin require Mochiweb web server
** username: guest password: guest
Use php 5.3*
Use php-amqplib, a php implementation of
amqp protocol from
https://github.com/videlalvaro/php-amqplib

$ git clone git://github.com/videlalvaro/php-amqplib.git

use composer to resolve depencies
$ curl --silent https://getcomposer.org/installer | php
$ php composer.phar install

* this version of lib require php 5.3 because it use namespaces feature for use it on php 5.2 checkout
from http://code.google.com/p/php-amqplib/ and use require_once instead of namespaces
Example:
I have a process that execute two operation:

1. Download and process some data (fast)
2. Download image from web (slow)

I don't want to wait for second operation,
because it's slow!

I delegate the second operation to RabbitMQ
Solve with RabbitMQ
Standard                      RabbitMQ
        init                            init


   download data                  download data
                                                          Queue
  download images                 processing data


   processing data                     end


        end                                                    C
                     Consumer: consumes the queued data


                     "Processing data" step insert link in queue
                     It will be disposed of by the consumer regardless
Publisher




            Connection + Channel for AMQP




            Fast operation, run directly


                      Put (publish) in Queue
                      link for download image
Consumer


           Connection + Channel for AMQP




             Process function (download images)
             this is a callback function used by
             basic_consume method for queue consume
             *

            When the process is completed consumer
            return an acknowledge to publisher
                       *
References

http://www.rabbitmq.com/                   Official Site
https://github.com/videlalvaro/php-amqplib Php API (5.3)
http://code.google.com/p/php-amqplib/      Php API (5.2)




                 Sergio Sicari <sergiosicari@gmail.com>

RabbitMQ in PHP

  • 1.
    RabbitMQ in PHP usemessage broker rabbitmq in php Sergio Sicari <sergiosicari@gmail.com>
  • 2.
    What is aRabbitMQ? RabbitMQ is a messaging broker, a service platform to send and receive messages implementing the Advanced Message Queueing Protocol (AMQP). It's write in Erlang and open source. It's official supported on a number of operating system and language (through API)
  • 3.
    Install the service OnUbuntu from repository: 1) Add the following line to your /etc/apt/sources.list: deb http://www.rabbitmq.com/debian/ testing main 2) $ apt-get update 3) $ sudo apt-get install rabbitmq-server You managing the service with 'rabbitmqctl' $ rabbitmqctl stop/start $ rabbitmqctl status
  • 4.
    Managment (rabbitmqadmin*) The rabbitmq-managementplugin provides an HTTP-based API for management and monitoring of your RabbitMQ server, along with a browser-based UI and a command line tool. You can install with follow command: $ rabbitmq-plugins enable rabbitmq_management Now you can manage RabbitMQ from web gui at: http://localhost:55672 ** * rabbitmqadmin require Mochiweb web server ** username: guest password: guest
  • 6.
    Use php 5.3* Usephp-amqplib, a php implementation of amqp protocol from https://github.com/videlalvaro/php-amqplib $ git clone git://github.com/videlalvaro/php-amqplib.git use composer to resolve depencies $ curl --silent https://getcomposer.org/installer | php $ php composer.phar install * this version of lib require php 5.3 because it use namespaces feature for use it on php 5.2 checkout from http://code.google.com/p/php-amqplib/ and use require_once instead of namespaces
  • 7.
    Example: I have aprocess that execute two operation: 1. Download and process some data (fast) 2. Download image from web (slow) I don't want to wait for second operation, because it's slow! I delegate the second operation to RabbitMQ
  • 8.
    Solve with RabbitMQ Standard RabbitMQ init init download data download data Queue download images processing data processing data end end C Consumer: consumes the queued data "Processing data" step insert link in queue It will be disposed of by the consumer regardless
  • 9.
    Publisher Connection + Channel for AMQP Fast operation, run directly Put (publish) in Queue link for download image
  • 10.
    Consumer Connection + Channel for AMQP Process function (download images) this is a callback function used by basic_consume method for queue consume * When the process is completed consumer return an acknowledge to publisher *
  • 11.
    References http://www.rabbitmq.com/ Official Site https://github.com/videlalvaro/php-amqplib Php API (5.3) http://code.google.com/p/php-amqplib/ Php API (5.2) Sergio Sicari <sergiosicari@gmail.com>