RabbitMQ in PHP


Published on

An Introduction to implementing rabbitmq solution in php

RabbitMQ in PHP

  1. 1. RabbitMQ in PHPuse message broker rabbitmq in php Sergio Sicari <sergiosicari@gmail.com>
  2. 2. What is a RabbitMQ?RabbitMQ is a messaging broker, a serviceplatform to send and receive messagesimplementing the Advanced MessageQueueing Protocol (AMQP).Its write in Erlang and open source.Its official supported on a number of operatingsystem and language (through API)
  3. 3. Install the serviceOn Ubuntu from repository:1) Add the following line to your /etc/apt/sources.list: deb http://www.rabbitmq.com/debian/ testing main2) $ apt-get update3) $ sudo apt-get install rabbitmq-serverYou managing the service with rabbitmqctl $ rabbitmqctl stop/start $ rabbitmqctl status
  4. 4. Managment (rabbitmqadmin*)The rabbitmq-management plugin provides an HTTP-basedAPI for management and monitoring of your RabbitMQserver, along with a browser-based UI and a command linetool.You can install with follow command:$ rabbitmq-plugins enable rabbitmq_managementNow you can manage RabbitMQ from web gui at:http://localhost:55672 *** rabbitmqadmin require Mochiweb web server** username: guest password: guest
  5. 5. Use php 5.3*Use php-amqplib, a php implementation ofamqp protocol fromhttps://github.com/videlalvaro/php-amqplib$ git clone git://github.com/videlalvaro/php-amqplib.gituse 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 checkoutfrom http://code.google.com/p/php-amqplib/ and use require_once instead of namespaces
  6. 6. Example:I have a process that execute two operation:1. Download and process some data (fast)2. Download image from web (slow)I dont want to wait for second operation,because its slow!I delegate the second operation to RabbitMQ
  7. 7. Solve with RabbitMQStandard 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
  8. 8. Publisher Connection + Channel for AMQP Fast operation, run directly Put (publish) in Queue link for download image
  9. 9. 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 *
  10. 10. Referenceshttp://www.rabbitmq.com/ Official Sitehttps://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>