Stomp Queue
Stomp is a simple (stream) text-oriented messaging protocol that provides an interoperable connection format allowing STOMP clients to communicate with any STOMP message broker. The workerman/stomp implements a Stomp client primarily for messaging queue scenarios such as RabbitMQ, Apollo, ActiveMQ, etc.
Installation
composer require webman/stomp
Configuration
The configuration file is located under config/plugin/webman/stomp
Sending Messages
<?php
namespace app\controller;
use support\Request;
use Webman\Stomp\Client;
class Index
{
public function queue(Request $request)
{
// Queue
$queue = 'examples';
// Data (when passing an array, you need to serialize it yourself, for example using json_encode, serialize, etc.)
$data = json_encode(['to' => 'tom@gmail.com', 'content' => 'hello']);
// Execute delivery
Client::send($queue, $data);
return response('redis queue test');
}
}
In order to be compatible with other projects, the Stomp component does not provide automatic serialization and deserialization. If the data being sent is an array, you need to serialize it yourself, and deserialize it yourself when consuming.
Consuming Messages
Create a new file app/queue/stomp/MyMailSend.php (the class name can be anything, as long as it complies with the psr4 specification).
<?php
namespace app\queue\stomp;
use Workerman\Stomp\AckResolver;
use Webman\Stomp\Consumer;
class MyMailSend implements Consumer
{
// Queue name
public $queue = 'examples';
// Connection name, corresponding to the connection in stomp.php
public $connection = 'default';
// If the value is client, you need to call $ack_resolver->ack() to inform the server that it has been successfully consumed
// If the value is auto, there is no need to call $ack_resolver->ack()
public $ack = 'auto';
// Consume
public function consume($data, AckResolver $ack_resolver = null)
{
// If the data is an array, you need to deserialize it yourself
var_export(json_decode($data, true)); // Outputs ['to' => 'tom@gmail.com', 'content' => 'hello']
// Inform the server that it has been successfully consumed
$ack_resolver->ack(); // This call can be omitted if ack is auto
}
}
Enable STOMP Protocol in RabbitMQ
RabbitMQ does not have the STOMP protocol enabled by default, you need to execute the following command to enable it
rabbitmq-plugins enable rabbitmq_stomp
After enabling, the default port for STOMP is 61613.