How to use php-amqplib rabbitMQ in a web browser

I am trying to use php-amqplib to send and receive a message. It works by sending / receiving to the terminal. But when you switch to a web browser, you cannot receive from the queue, it is constantly waiting for a message. I used below code to get .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 receives the first message from the queue if I use below instead of the callback function, but does not consume from the queue

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

This means that messages are available in the queue 'test22'. give me any hint.

+6
source share
1 answer

Change echo $msg->body; on error_log($msg->body); (or other registration system that you use). I think you'll probably see log messages. The page is already loaded in the web browser, so it will not change even if the script receives the message.

+1
source

Source: https://habr.com/ru/post/980764/


All Articles