mjohann/simple-rabbitmq

Simple RabbitMQ is a PHP class that offers a clean and reusable abstraction for working with RabbitMQ. It simplifies the process of connecting to message queues and performing common operations like publishing messages, consuming queues, acknowledging deliveries, and managing exchanges and bindings

v1.0.0 2025-04-16 03:35 UTC

This package is auto-updated.

Last update: 2025-04-16 03:36:41 UTC


README

Simple RabbitMQ is a PHP class that offers a clean and reusable abstraction for working with RabbitMQ. It simplifies the process of connecting to message queues and performing common operations like publishing messages, consuming queues, acknowledging deliveries, and managing exchanges and bindings β€” all without having to deal with the complexity of the underlying configuration.

πŸ“¦ Installation

You can install the library via Packagist/Composer:

composer require mjohann/simple-rabbitmq

βš™οΈ Requirements

  • PHP 7.4 or higher

πŸš€ Features

  • Simple RabbitMQ uses enqueue/amqp-lib as a dependency
  • Supported:
    • config β€” Configures the connection parameters (e.g., host, port, credentials).
    • open β€” Opens a connection to the RabbitMQ server.
    • close β€” Closes the connection.
    • exchange($name) β€” Declares an exchange.
    • queue($name) β€” Declares a queue.
    • queueBind() β€” Binds a queue to an exchange.
    • pub_exchange($message) β€” Publishes a message to the exchange.
    • sub($callback) β€” Subscribes a callback function to consume messages from the queue.
    • waitCallbacks($milliseconds) β€” Waits for messages and dispatches them to the subscribed callbacks for a given duration.
    • readMessage() β€” (Commented out, but exists) Likely reads a single message from the queue.
    • acknowledge($message) β€” Acknowledges receipt of a message (seen in the consumer callback via $consumer->acknowledge()).

πŸ§ͺ Usage Example

Publisher

<?php

require_once "vendor/autoload.php";

use MJohann\Packlib\SimpleRabbitMQ;

// Create a new instance of the SimpleRabbitMQ class
$srmq = new SimpleRabbitMQ();

// Configure connection parameters
$srmq->config();

// Open the connection to the RabbitMQ server
$srmq->open();

// Declare an exchange named "my_exchange"
$srmq->exchange("my_exchange");

// Declare a queue named "my_queue"
$srmq->queue("my_queue");

// Bind the declared queue to the exchange
$srmq->queueBind();

// Publish 30,000 messages to the exchange
for ($i = 1; $i <= 30000; $i++) {
    $srmq->pub_exchange("test " . $i); // Publish a message with a test string
    echo $i, PHP_EOL; // Output the message number to the console
}

// Close the connection to the RabbitMQ server
$srmq->close();

Subscriber

<?php

require_once "vendor/autoload.php";

use MJohann\Packlib\SimpleRabbitMQ;

// Create a new instance of the SimpleRabbitMQ class
$srmq = new SimpleRabbitMQ();

// Configure connection parameters
$srmq->config();

// Open the connection to the RabbitMQ server
$srmq->open();

// Declare an exchange named "my_exchange"
$srmq->exchange("my_exchange");

// Declare a queue named "my_queue"
$srmq->queue("my_queue");

// Bind the declared queue to the exchange
$srmq->queueBind();

// Define the first callback function to handle incoming messages
$callback = function ($message, $consumer) {
    echo "Message: ", $message->getBody(), PHP_EOL; // Print the message content
    $consumer->acknowledge($message); // Acknowledge the message to RabbitMQ
    return true; // Return true to continue listening
};

// Subscribe the first callback to the queue
$srmq->sub($callback);

// Optionally, read a single message manually (commented out)
// $srmq->readMessage();

// Wait for messages and trigger callbacks for up to 15 seconds
$srmq->waitCallbacks(15000); // 15000 milliseconds = 15 seconds

// Close the connection to the RabbitMQ server
$srmq->close();

For more examples, see the example/ file in the repository.

πŸ“ Project Structure

simple-rabbitmq/
β”œβ”€β”€ src/
β”‚   └── SimpleRabbitMQ.php
β”œβ”€β”€ example/
β”‚   └── docker-compose.php
β”‚   └── sub.php
β”‚   └── pub.php
β”œβ”€β”€ composer.json
β”œβ”€β”€ .gitignore
β”œβ”€β”€ LICENSE
└── README.md

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for more information.

πŸ‘¨β€πŸ’» Author

Developed by Matheus Johann AraΓΊjo – Pernambuco, Brazil.