We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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 https://www.rabbitmq.com/tutorials/tutorial-one-php.html. It seems that

$conn = new AMQPConnection($conn_args);

doesn't work.



98.9k

Are you getting an error/exception or something?

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

$conn = new AMQPConnection($conn_args);

doesn't work.



6.9k
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 https://www.rabbitmq.com/tutorials/tutorial-one-php.html. It seems that

$conn = new AMQPConnection($conn_args);

doesn't work.