The use of multiple servers with dedicated functionality can communicate with each other through API and notifications.
Overview
Example where a backend server that performs a task followed by notification to user via Email, while to another server through PUSH notification.
The push allows communication between 2 computer systems through an agreed protocol. RabbitMQ is a message-queueing software also known as a message broker or queue manager, that provides such a service, implementing protocols AMQP 1.0 and MQTT 5. It is can be used under the Apache License 2.0 and Mozilla Public License 2.- A producer: Sends a message to RabbitMQ with a specified exchange (direct, topic, or fanout) and queue(s) name.
- RabbitMQ: Places the message to the queue(s).
- A consumer: Configured to retrieve message from a queue.
- A consumer: Check and retrieve message from the queue.
- RabbitMQ: Remove message from the queue
- Install RabbitMQ
- Configure RabbitMQ
- Add RabbitMQ package in a Laravel project
- Create a service of Laravel jobs or other automated function
- Create Laravel controller to publish and consume messages
- Tinker to test Producer
- Tinker to test Consumer
Install RabbitMQ
- RabbitMQ server: IP 10.1.1.101
- Producer server: IP 10.1.1.102
- Consumer server: IP 10.1.1.103
On RabbitMQ, install the server application.
curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.rpm.sh | sudo bashcurl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bashsudo yum makecache -y --disablerepo='*' --enablerepo='rabbitmq_rabbitmq-server'
sudo yum -y --disablerepo='*' --enablerepo='rabbitmq_rabbitmq-server' --enablerepo='rabbitmq_erlang' install rabbitmq-server
rpm -qi rabbitmq-server
Version : 3.13.0
Release : 1.el8
Architecture: noarch
Configure the server
echo "127.0.0.1 rabbitmq.demo" | sudo tee -a /etc/hostssudo systemctl enable --now rabbitmq-server.servicesudo rabbitmqctl statussudo rabbitmq-plugins enable rabbitmq_managementss -tunelp | grep 15672sudo firewall-cmd --add-port={5672,15672}/tcp --permanentsudo firewall-cmd --reload
Create users
Laravel support for rabbitmq
composer require php-amqplib/php-amqplibcomposer updatesudo semanage port -a -t http_port_t -p tcp 5672
RABBITMQ_HOST=mem.hqcloakRABBITMQ_IP=10.1.1.101RABBITMQ_PORT=5672RABBITMQ_VHOST="/"RABBITMQ_LOGIN=user1RABBITMQ_PASSWORD=SECRETPASSWORDRABBITMQ_QUEUE="queue1"
Laravel rabbitmq service
Create Laravel Controller
Create a tinker to test Producer
$controller = app()->make('App\Http\Controllers\API\RabbitMQController');$results = app()->call([$controller, 'publish'], ['message'=>'1001|ACTION|VALUE'] );print( "$results");
Run tinker
more tinker-producer.php | php artisan tinker
On RabbitMQ web console, observer creation of the queue and the message.
Create a tinker to test Consumer
$controller = app()->make('App\Http\Controllers\RabbitMQController');$results = app()->call([$controller, 'consume'],[] );print( "$results");
Run tinker
more tinker-consumer.php | php artisan tinker
On RabbitMQ web console, observer queue and message consumed.