Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use php-amqplib rabbitMQ on web browser

I am trying to use php-amqplib for sending and receiving message. It works sending/receiving on terminal. But When go for web browser, unable to receive from queue it continuously waits for message. I used below code for receive.php

require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
include_once(__DIR__ . '/config/config.php');
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $connection->channel();
$channel->queue_declare('test22');    
$callback = function($msg){
echo $msg->body;
};    
$channel->basic_consume('test22', 'consumer_tag', false, true, false, false, $callback);

while(count($channel->callbacks)) {
$channel->wait();
}    
$channel->close();
$connection->close();

It gets first message from queue if I use below instead of callback function but does not consume from queue

$abc=$channel->basic_get("test22", false, 2);    
if(!empty($abc))
{
print_r($abc->body);
} 

It means messages are available in queue 'test22'. give me any clue.

like image 994
Prak Avatar asked Jan 09 '15 13:01

Prak


People also ask

How do I connect to RabbitMQ server?

In order for a client to interact with RabbitMQ it must first open a connection. This process involves a number of steps: Application configures the client library it uses to use a certain connection endpoint (e.g. hostname and port) The library resolves the hostname to one or more IP addresses.

What is RabbitMQ PHP?

RabbitMQ : It is an open source message broker software. You might have heard Messaging Service by which two PHP applications can communicate with each other. Using RabbitMQ message broker two PHP applications can interact with each other in same way as we human being do by text message.

How do I get my RabbitMQ queue messages?

In rabbitmq, we can read or consume a published messages from queue using web management portal for that we need to login into rabbitmq web management portal using default (guest) credentials like as shown below.


1 Answers

Change echo $msg->body; to error_log($msg->body); (or other loggin system you're using). I think you'll probably will see the messages on the logs. On the web browser the page is already loaded so it won't change even if the script is receiving the message.

like image 157
Alberto Garrido Avatar answered Oct 12 '22 22:10

Alberto Garrido