PHP code example of saxulum / saxulum-message-queue
1. Go to this page and download the library: Download saxulum/saxulum-message-queue library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
saxulum / saxulum-message-queue example snippets
{.php}
namespace My\Project;
use Saxulum\MessageQueue\MessageInterface;
class SampleMessage implements MessageInterface
{
/**
* @var string
*/
private $context;
/**
* @var string
*/
private $message;
/**
* @param string $context
* @param string $message
*/
public function __construct(string $context, string $message)
{
$this->context = $context;
$this->message = $message;
}
/**
* @param string $json
*
* @return MessageInterface
*/
public static function fromJson(string $json): MessageInterface
{
$rawMessage = json_decode($json);
return new self($rawMessage->context, $rawMessage->message);
}
/**
* @return string
*/
public function toJson(): string
{
return json_encode([
'context' => $this->context,
'message' => $this->message,
]);
}
/**
* @return string
*/
public function getContext(): string
{
return $this->context;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
}
{.json}
{
"amqplib/php-amqplib": "^2.6.3"
}
}
{.php}
use My\Project\SampleMessage;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use Saxulum\MessageQueue\Redis\RedisReceive;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$receive = new RabbitMQReceive(SampleMessage::class, $connection, 'messages');
$message = $sender->receive();
{.php}
use My\Project\SampleMessage;
use Saxulum\MessageQueue\SystemV\SystemVSend;
$sender = new SystemVSend(1);
$sender->send(new SampleMessage('context', 'this is a message'));
{.php}
use My\Project\SampleMessage;
use Saxulum\MessageQueue\SystemV\SystemVReceive;
$sender = new SystemVReceive(SampleMessage::class, 1);
$message = $sender->receive();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.