1. Go to this page and download the library: Download crazy-goat/rabbit-stream library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use CrazyGoat\RabbitStream\Client\Connection;
use CrazyGoat\RabbitStream\VO\OffsetSpec;
$connection = Connection::create(host: 'localhost', port: 5552);
$consumer = $connection->createConsumer('my-stream', offset: OffsetSpec::first());
while ($messages = $consumer->read(timeout: 5)) {
foreach ($messages as $msg) {
echo $msg->getBody() . "\n";
}
}
$consumer->close();
$connection->close();
use CrazyGoat\RabbitStream\Client\Connection;
use CrazyGoat\RabbitStream\Client\ConfirmationStatus;
// Connect (handshake and authentication handled automatically)
$connection = Connection::create(
host: '127.0.0.1',
user: 'guest',
password: 'guest'
);
// Create a producer for 'my-stream'
$producer = $connection->createProducer(
stream: 'my-stream',
onConfirm: function (ConfirmationStatus $status): void {
if ($status->isConfirmed()) {
echo "Message {$status->getPublishingId()} confirmed\n";
}
}
);
// Send a message
$producer->send("Hello, RabbitMQ Stream!");
// Drive the loop to receive confirmations (optional, blocking)
$connection->readLoop(maxFrames: 1);
// Close producer and connection
$producer->close();
$connection->close();
use CrazyGoat\RabbitStream\Client\AmqpMessageDecoder;
use CrazyGoat\RabbitStream\Client\OsirisChunkParser;
// ... subscribe to stream and receive Deliver response
$chunk = $deliverResponse->getChunk();
$entries = OsirisChunkParser::parse($chunk);
// Decode AMQP 1.0 messages into Message objects
$messages = AmqpMessageDecoder::decodeAll($entries);
foreach ($messages as $message) {
echo "Offset: {$message->getOffset()}\n";
echo "Body: {$message->getBody()}\n";
echo "Content-Type: {$message->getContentType()}\n";
echo "Message-ID: {$message->getMessageId()}\n";
}
use CrazyGoat\RabbitStream\Client\Connection;
use CrazyGoat\RabbitStream\VO\OffsetSpec;
$connection = Connection::create(host: 'localhost', port: 5552);
// Named consumer with auto-commit every 100 messages
// The name is used to persist the offset on the server
$consumer = $connection->createConsumer(
stream: 'my-stream',
offset: OffsetSpec::first(),
name: 'my-consumer-group',
autoCommit: 100,
);
while ($messages = $consumer->read(timeout: 5)) {
foreach ($messages as $msg) {
echo $msg->getBody() . "\n";
}
}
$consumer->close(); // stores final offset automatically
// On next startup, resume from the stored offset
$storedOffset = $connection->queryOffset('my-consumer-group', 'my-stream');
$consumer = $connection->createConsumer(
stream: 'my-stream',
offset: OffsetSpec::offset($storedOffset + 1),
name: 'my-consumer-group',
autoCommit: 100,
);
$connection->close();
use CrazyGoat\RabbitStream\StreamConnection;
use CrazyGoat\RabbitStream\Request\PeerPropertiesRequestV1;
...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.