[rabbitmq-discuss] how to call a model from rabbitmq php consumer's callback in codeigniter?

Seshachalam Malisetti abbiya at gmail.com
Thu Apr 25 07:30:05 BST 2013

I developed an android app where it subscribes to a queue and also 
publishes to other queues. At a time it publishes the same message to two 
different queues, one of them is a queue named "Queue" and now from a 
appfog instance i need to subscribe to the "Queue" and consume messages and 
insert them in a mysql db.

I created a php standalone app for the above purpose with codeigniter. By 
some reason the worker app loses its connection to rabbitmq. i would like 
to know the best way to do this. How can a worker app on appfog can sustain 
the application restarts.

what of kind of thing i need to use to solve the above problem.

I figured that the problem is not with rabbitmq connection. it is with the 
code related to mysql inserts. i checked the crash logs of my app and the 
error is "Mysql gone away". an example of php rabbitmq consumer has call 
backs for receive message and register_shutdown. in receive call back i can 
not use $this of code igniter because its out of scope and i was using 
get_instance(). i am not sure how to call a method from rabbitmq client 
receive call back function

The controller is 

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

include(__DIR__ . '/php-amqplib/config.php');
use PhpAmqpLib\Connection\AMQPConnection;
class Welcome extends CI_Controller {
public function __construct() {
public function index() {
    //connect to rabbitmq and consume messages
    //insert messages to mysql
    //$this->messages = array();
    $exchange = "router";
    $queue = "abbiya";

    $conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
    $ch = $conn->channel();

      name: $queue
      passive: false
      durable: true // the queue will survive server restarts
      exclusive: false // the queue can be accessed in other channels
      auto_delete: false //the queue won't be deleted once the channel is closed.
    $ch->queue_declare($queue, false, true, false, false);

    $ch->queue_bind($queue, $exchange, $queue);

      queue: Queue from where to get the messages
      consumer_tag: Consumer identifier
      no_local: Don't receive messages published by this consumer.
      no_ack: Tells the server if the consumer will acknowledge the messages.
      exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
      callback: A PHP Callback
    $consumer_tag = "abbiya";

    $ch->basic_consume($queue, $consumer_tag, false, false, false, false, function($msg) {
                $message_body = json_decode($msg->body);

                // Send a message with the string "quit" to cancel the consumer.
                if ($msg->body === 'quit') {
                $data = array(
                    'sender_id' => $message_body->r,
                    'receiver_id' => $message_body->s,
                    'message_content' => $message_body->m,
                    // 'sent_time' => $message_body->t,
                    'status' => 0
                $ci =& get_instance();

    // Loop as long as the channel has callbacks registered
    while (count($ch->callbacks)) {

    register_shutdown_function(function() use ($ch, $conn) {
/* End of file welcome.php *//* Location: ./application/controllers/welcome.php */

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130424/5393b3fc/attachment.htm>

More information about the rabbitmq-discuss mailing list