What is the best practice, to receive Data from a queue every second via php? I do this with an ajax query, what calls the php script every second. There, a connection object is created and a queue is declared every time. I tried to save this after the first time in a session variable, but when I call the PHP script a second time, I can't receive any more data. When I debug the channel object, I see that is_open is false:
protected' is_open' => boolean false
Here is my basic php test code:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
session_start(); # start session handling.
$id = $_GET["uid"];
$connected = $_GET["connected"];
if (empty($id)) {
$id = 0;
}
$queue = 'CyOS EV Queue ' . $id;
$reset = $_GET["reset"];
if ($reset === "true") {
session_destroy();
$_SESSION = array();
echo "session destroyed";
var_dump($_SESSION);
exit;
}
$connection;
$channel;
if (!isset($_SESSION['coneccted'])) {
$_SESSION['coneccted'] = true;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare($queue, false, false, false, false, false);
$channel->queue_bind($queue, 'CyOS-EX');
$_SESSION['connection'] = $connection;
$_SESSION['channel'] = $channel;
} else {
echo "already connected \n\r";
$connection = $_SESSION['connection'];
$channel = $_SESSION['channel'];
var_dump($_SESSION);
}
$test = new AMQPMessage();
while ($i < 10) {
echo "try to get data from " . $queue . "\n\r";
$test = $channel->basic_get($queue, true);
$i++;
if (isset($test)) {
echo "received data";
break;
}
}
echo $test->body;
When I initilize the connection and the channel every time I call the script then it works.
I presume the lines you are concerned about are these ones:
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare($queue, false, false, false, false, false);
$channel->queue_bind($queue, 'CyOS-EX');
Let's look at what's happening here:
The normal way to avoid re-connecting (steps 1 and 2) is to have the consumer running in the background, e.g. starting a command-line PHP script using supervisord which runs continuously processing messages as they come in. However, that won't work if you need to get data back to the browser once it appears in the queue.
Common alternatives to polling and creating a new PHP process each time include:
As I say, these are not specific to RabbitMQ, but apply to any time you're waiting for something to happen in a database, a file, a remote service, etc.