how to call a model from rabbitmq php consumer's callback in codeigniter?

Skip to first unread message

Seshachalam Malisetti

Apr 25, 2013, 2:30:59 AM4/25/13
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 */


May 22, 2013, 9:31:23 AM5/22/13
1.when you send data form you apply jquery to send to other form too. 

2013/4/25 Seshachalam Malisetti <>

You received this message because you are subscribed to the Google Groups "codeigniter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Reply all
Reply to author
0 new messages