Last active
June 4, 2019 07:45
-
-
Save heilgar/bea1028437195fd3fe2abce2b1d5e8c6 to your computer and use it in GitHub Desktop.
PHP RabbitMQ Example for thewebland.net used docker-compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "thewebland/rabbitmq-php", | |
"description": "Usage example of RabbitMQ with php for thewebland.net", | |
"require": { | |
"php-amqplib/php-amqplib": "^2.9" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.0' | |
services: | |
rabbitmq: | |
image: rabbitmq:3.7.15-alpine | |
ports: | |
- "5672:5672" | |
environment: | |
RABBITMQ_DEFAULT_USER: "rabbitmq" | |
RABBITMQ_DEFAULT_PASS: "rabbitmq" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use PhpAmqpLib\Connection\AMQPStreamConnection; | |
$connection = new AMQPStreamConnection('localhost', 5672, 'rabbitmq', 'rabbitmq'); | |
$channel = $connection->channel(); | |
$channel->queue_declare('hello', false, false, false, false); | |
echo " [*] Waiting for messages. To exit press CTRL+C\n"; | |
$callback = function ($msg) { | |
echo ' [x] Received ', $msg->body, "\n"; | |
}; | |
$channel->basic_consume('hello', '', false, true, false, false, $callback); | |
while (count($channel->callbacks)) { | |
$channel->wait(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use PhpAmqpLib\Connection\AMQPStreamConnection; | |
use PhpAmqpLib\Message\AMQPMessage; | |
$connection = new AMQPStreamConnection('localhost', 5672, 'rabbitmq', 'rabbitmq'); | |
$channel = $connection->channel(); | |
$channel->queue_declare('hello', false, false, false, false); | |
$cliMessage = getopt(null, ["message:"]); | |
$msg = new AMQPMessage($cliMessage['message']); | |
$channel->basic_publish($msg, '', 'hello'); | |
echo " [x] Sent '{$cliMessage["message"]}'\n"; | |
$channel->close(); | |
$connection->close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0. docker-compose up -d | |
1. composer install | |
2. php receive.php | |
3. php send.php --message="Hello, World" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment