Download the PHP package byjg/rabbitmq-client without Composer
On this page you can find all versions of the php package byjg/rabbitmq-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download byjg/rabbitmq-client
More information about byjg/rabbitmq-client
Files in byjg/rabbitmq-client
Package rabbitmq-client
Short Description It creates a simple abstraction layer to publish and consume messages from the RabbitMQ Server using the component byjg/message-queue-client.
License MIT
Informations about the package rabbitmq-client
sidebar_key: rabbitmq-client sidebar_category: messaging tags: [php, queue]
RabbitMQ Client
It creates a simple abstraction layer to publish and consume messages from the RabbitMQ Server using the component byjg/message-queue-client.
For details on how to use the Message Queue Client see the documentation
Why
This is the RabbitMQ connector of byjg/message-queue-client.
Your code publishes and consumes through the same ConnectorInterface whatever the
broker, so it does not depend on RabbitMQ: switching to another broker, or to the
in-memory MockConnector in tests, means a different connection URI (amqp://… →
redis://… or mock://…), not a rewrite.
Installation
Usage
Publish
Consume
The consume method will wait for a message and call the callback function to process the message. If there is no message in the queue, the method will wait until a message arrives.
If you want to exit the consume method, just return Message::ACK | Message::EXIT from the callback function.
Possible return values from the callback function:
Message::ACK- Acknowledge the message and remove from the queueMessage::NACK- Not acknowledge the message and remove from the queue. If the queue has a dead letter queue, the message will be sent to the dead letter queue.Message::REQUEUE- Requeue the messageMessage::EXIT- Exit the consume method
RabbitMQ Client (AMQP Protocol)
The RabbitMQ connector uses the php-amqplib library.
The standard behavior of the connector is to create an Exchange, a Queue and bind the queue to the exchange with a routing key (by default is the same as the queue name). All messages are published to the exchange and consumed from the queue.
As the queue and exchange is created by the Connector it is recommended you do not use to publish/consume from existing queues. If you use an existing Queue you might get the error:
You can change the behavior of the connection by using the Pipe::withProperty() and Message::withProperty() methods.
Some of them are used by the RabbitMQConnector by setting some default values:
Pipe::withProperty(RabbitMQConnector::EXCHANGE)- Set the exchange name. Default is the queue name.Pipe::withProperty(RabbitMQConnector::ROUTING_KEY)- Set the routing key. Default is the queue name.Pipe::withProperty('exchange_type')- Set the exchange type. Default isdirect; a dead letter queue always usesfanout.Pipe::withProperty('x-message-ttl')- Passed as a queue argument. Set the time to live of the message in milliseconds. No default.Pipe::withProperty('x-expires')- Passed as a queue argument. Set the time to live of the queue in milliseconds. No default.Message::withProperty('content_type')- Set the content type of the message. Default is text/plain.Message::withProperty('delivery_mode')- Set the delivery mode of the message. Default is 2 (persistent).Message::withProperty('priority')- Set the priority of the message. Values range from 0 to 255, but it's recommended to use 0-10 for performance. Higher values have higher priority.Pipe::withProperty('x-max-priority')- Set the maximum priority level for the queue. Required for priority queues to work.
Priority Queues
RabbitMQ supports message prioritization, allowing higher priority messages to be delivered before lower priority ones. To use priority queues:
-
Declare the queue with a maximum priority level:
-
Set priority on individual messages:
- Publish and consume as normal:
Important notes about RabbitMQ message priorities:
- The queue MUST be declared with the 'x-max-priority' argument for priorities to work
- Valid priority values range from 0 to 255, but RabbitMQ recommends using 0-10 for performance
- Higher priority values have higher priority (10 is higher priority than 1)
- Messages with the same priority are processed in FIFO order (first in, first out)
- If no priority is set, the message defaults to priority 0 (lowest)
For more details, see the Priority Queues documentation.
Protocols:
| Protocol | URI Example | Notes |
|---|---|---|
| AMQP | amqp://user:pass@host:port/vhost?arg1=...&args=... | Default port: 5672. |
| AMQPS | amqps://user:pass@host:port/vhost?arg1=...&args=... | Default port: 5671. Required: capath |
Connection Parameters
The following parameters are available for both AMQP and AMQPS connections:
- heartbeat: Interval in seconds to send heartbeat frames to keep the connection alive during periods of inactivity. Default is 30 seconds.
- connection_timeout: Timeout in seconds when establishing a new connection. Default is 40 seconds.
- max_attempts: Maximum number of reconnection attempts before failing. Default is 10 attempts.
- pre_fetch: Controls how many messages the server will deliver before requiring acknowledgements. Default is 0 (no limit).
- timeout: Specifies the timeout in seconds for waiting for messages when consuming. Default is 600 seconds.
- single_run: When set to 'true',
consume()returns after the first consumption cycle ends (e.g. thetimeoutelapses with no message) instead of reconnecting and waiting again. Default is 'false'.
For more details and examples, see the Connection Parameters documentation.
AMQPS SSL Parameters
The following parameters are available for secure connections via AMQPS:
- capath: (Required) Path to the CA certificates: a directory, or a single CA certificate file. This parameter is mandatory for AMQPS connections.
- local_cert: Path to the client certificate file.
- local_pk: Path to the client private key file.
- verify_peer: Enable/disable peer verification (true/false). Default: true.
- verify_peer_name: Enable/disable peer name verification (true/false). Default: true.
- passphrase: The passphrase for the private key.
- ciphers: A list of ciphers to use for the encryption.
For more details and examples, see the SSL/TLS Configuration documentation.
Robust Connection Setup
For applications that need to maintain long-lived connections to RabbitMQ, especially in environments with network challenges or when the connection remains idle for extended periods, use the following robust connection settings:
This configuration:
- Sends heartbeat every 30 seconds to keep the connection alive
- Sets a 10-second timeout when establishing the connection
- Will attempt to reconnect up to 5 times with exponential backoff
- Sets a 60-second timeout for message consumption operations
For more details, see the Robust Connection Setup documentation.
Running Tests
All versions of rabbitmq-client with dependencies
ext-curl Version *
byjg/message-queue-client Version ^6.0
php-amqplib/php-amqplib Version ^3.5