RabbitMQ
{
"require": {
"php-amqplib/php-amqplib": ">=3.0"
}
}
Block docker-composer:
rebbitmq:
image: rabbitmq:3-management
ports:
- "5672:5672"
- "15672:15672"
# login: guest
# password: guest
Отправка сообщения на PHP:
class Rabbit
{
private string $host = '192.168.0.107';
private int $port = 5672;
private string $user = 'guest';
private string $password = 'guest';
private string $queueName = 'Testing';
private string $exchangeName = 'test';
private AMQPStreamConnection $connection;
private AMQPMessage $message;
private string $text;
private function __construct()
{
$this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password);
$this->message = new AMQPMessage($text??'', ['content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
}
public static function send($text): void
{
$model = new self();
$channel = $model->connection->channel();
$channel->exchange_declare($model->exchangeName, 'direct', false, true, false);
$channel->queue_declare($model->queueName, false, false, false, false);
$channel->queue_bind($model->queueName, $model->exchangeName);
$channel->basic_publish($model->message, '', $model->queueName);
$channel->close();
$model->connection->close();
}
}
Отправка сообщения на js:
Установка пакета:
npm install amqplib
Отправка сообщения с консоли:
const amqp = require('amqplib/callback_api');
process.stdin.on('readable', function() {
const msg = process.stdin.read();
if (msg !== null) {
amqp.connect('amqp://192.168.0.107:5672', function(error0, connection) {});
amqp.connect('amqp://192.168.0.107:5672', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {});
});
amqp.connect('amqp://192.168.0.107:5672', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
const queue = 'TestQueue';
channel.assertQueue(queue, {
durable: false
});
channel.sendToQueue(queue, Buffer.from(msg));
console.log(" [x] Sent %s", msg);
setTimeout(function() {
connection.close();
process.exit(0)
}, 500);
});
});
}
});
Прием на js:
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://192.168.0.107:5672', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
var queue = 'TestQueue';
channel.assertQueue(queue, {
durable: false
});
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
channel.consume(queue, function(msg) {
console.log(" [x] Received %s", msg.content.toString());
}, {
noAck: true
});
});
});
Pusher
Создаем сообщение на PHP:
class Pusher
{
private const app_id = "";
private const key = "";
private const secret = "";
private const options = ['cluster' => 'eu', 'useTLS' => true];
private static ?object $instance = null;
private function __construct()
{
}
private function __clone(): void
{
// TODO: Implement __clone() method.
}
public static function getInstance(): ?object
{
if (self::$instance === null) {
self::$instance = new Push(
self::key,
self::secret,
self::app_id,
self::options
);
}
return self::$instance;
}
public static function send(string $message = 'test'): void
{ \Yii::$app->redis->rpush('my-channel:my-event:string', $message);
self::getInstance()->trigger('my-channel', 'my-event', $message);
}
Прослушиваем с помощью Vue3:
public function actionPusher(): string
{
if (Yii::$app->request->isPost) {
$object = (Yii::$app->request->post()['body']);
$text = Json::encode($object);
Pusher::send($text);
return 'ok';
} else {
return 'error';
}
}
const app = Vue.createApp({
data() {
return {
status: 0,
text: '',
my_id: "<?= $user->id ?? '' ?>",
messages: [],
}
},
methods: {
sendMessage() {
let username = "<?= $user->username ?? '' ?>";
let message = this.text;
let id = this.my_id;
let date = new Date();
let csfr = "<?= Yii::$app->request->getCsrfToken() ?>";
$.ajax(
{
url: "<?= Yii::$app->request->baseUrl . '/chat/pusher' ?>",
type: 'POST',
data: {
body: {
id: id,
name: username,
message: message,
date: date
}, _csfr: csfr
},
success: function () {
console.log('200');
},
error: function (err) {
console.log(err.message);
}
}
);
this.text = '';
}
}
}).mount('#app');