Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Last active January 12, 2021 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IgorDePaula/699b92ee5431ea5789b8f12b32ae914c to your computer and use it in GitHub Desktop.
Save IgorDePaula/699b92ee5431ea5789b8f12b32ae914c to your computer and use it in GitHub Desktop.
Rabbitmq service
<?php
namespace App\Services;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class Rabbitmq
{
private $channel, $connection, $queueName = null;
public function __construct($queueName)
{
$this->queueName = $queueName;
$this->connection = new AMQPStreamConnection(env('QUEUE_HOST'), env('QUEUE_PORT'), env('QUEUE_USER'), env('QUEUE_PASS'));
$this->channel = $this->connection->channel();
$this->channel->queue_declare($queueName, false, true, false, false);
}
public function sendMessage($message = null)
{
$msg = new AMQPMessage($message);
$this->channel->basic_publish($msg, '', $this->queueName);
}
public function consume(callable $callback)
{
$this->channel->basic_consume($this->queueName, '', false, true, false, false, $callback);
while ($this->channel->is_consuming()) {
$this->channel->wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment