How to use php-amqplib rabbitMQ on web browser
Asked Answered
W

1

8

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.

Wiliness answered 9/1, 2015 at 13:15 Comment(3)
What version of the library are you using? This file amqp.inc is not used anymore in modern versions of the library. I suggest you use this one: github.com/videlalvaro/php-amqplibBloodletting
You'd better use php third-party through getcomposer.org.Infinitude
Your receive.php / consumer should only be run via terminal as a process. It should then pull messages sent from both the terminal or browser run scripts.Unlearn
F
2

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.

Fortunate answered 16/1, 2015 at 19:31 Comment(1)
Excellent answer. I had the same problem as @Prak; your answer solved my problem.Sommer

© 2022 - 2024 — McMap. All rights reserved.