Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Can Phalcon use rabbitmq? If can, how?

edited Nov '14

so many thanks, but I still can't figure out how to use it with phalcon framework.

I just do it by reference with http://www.rabbitmq.com/tutorials/tutorial-one-php.html. It seems that

$conn = new AMQPConnection($conn_args);

doesn't work.



82.3k

Are you getting an error/exception or something?

I just do it by reference with http://www.rabbitmq.com/tutorials/tutorial-one-php.html. It seems that

$conn = new AMQPConnection($conn_args);

doesn't work.



6.3k
Accepted
answer

make sure you are requiring vendor autoload?

    require BASE_DIR . "/vendor/autoload.php";

I'm not sure if you obfuscated your connection params but AMQPConnection takes 4 params. I prefer to have the Phalcon DI load mine so here is an example:

/**
 * register a lazy loaded singleton queue service
 */
$di->setShared('queue', function() use ($config) {
    $connection = new \PhpAmqpLib\Connection\AMQPConnection($config->rabbit->host, $config->rabbit->port, $config->rabbit->username,$config->rabbit->password);
    return $connection;
});

Then from a plugin or a model or whever you want to create your notice:

        $channel = $this->getDi()->getShared('queue')->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.
        */
        $channel->queue_declare('my-queue-name', false, true, false, false);

        $channel->basic_qos(null, 1, null);

        $msg = new \PhpAmqpLib\Message\AMQPMessage('message to be sent', array(
                'delivery_mode' => 2, //make messages persistent
            )
        );

        $channel->basic_publish($msg, '', 'my-queue-name');

        $channel->close();

No, but its response is 500 internal server error

Are you getting an error/exception or something?

I just do it by reference with http://www.rabbitmq.com/tutorials/tutorial-one-php.html. It seems that

$conn = new AMQPConnection($conn_args);

doesn't work.