Message Brokers
Learn about message brokers in PHP.
We'll cover the following
Pertinent in the discussion on distributed systems are message brokers. Their value lies in their capability to enable scalable messaging. A message broker is an architecture that permits data exchange among various network components in a producer-consumer model. The inherently distributed nature of message brokers decouples application components. These are specifically message publishers and consumers with respective roles of message generation and subscription.
RabbitMQ message broker
Most message brokers used in production today employ the Advanced Messaging and Queueing Protocol (AMQP), an asynchronous standard for passing messages. One popular implementation of AMQP-supported messaging is RabbitMQ, an Erlang-OTP-powered technology which, in addition to enabling core messaging in Publish-Subscribe (pub-sub) fashion, allows for message routing and monitoring. RabbitMQ uses channels (TCP-connections) as routes for the data transmitted within its model, which is flexible enough to support various languages like PHP, Java, Python, and C#.
The next phase of the discussion is a simple pub-sub application powered by the RabbitMQ-compatible php-amqp
library maintained by Raul Araya. The focus of the following text is a simple console-based application that focuses on data transmission from one script to another using RabbitMQ.
The task
The example for this section is a bit redundant because it’s a rehash of an earlier example with some syntactic differences. The Fibonacci sequence is at this point a staple of the text but its value as a locus of functional programming examples cannot be understated. The producer in this case sends a range of integers to a consumer that processes the data it receives through a callback, converting it into a linear Fibonacci sequence.
Prominent in the code to follow is the php-amqp
library which does not require an installation of the AMQP extension. It abstracts a variety of AMQP functions such as queue creation, TCP handshake completion, as well as message broker connection.
The producer code looks like this:
Get hands-on with 1400+ tech skills courses.