Allows to use MongoDB as a message queue broker.
- Installation
- Create context
- Send message to topic
- Send message to queue
- Send priority message
- Send expiration message
- Send delayed message
- Consume message
$ composer require enqueue/mongodb
<?php
use Enqueue\Mongodb\MongodbConnectionFactory;
// connects to localhost
$connectionFactory = new MongodbConnectionFactory();
// same as above
$factory = new MongodbConnectionFactory('mongodb:');
// same as above
$factory = new MongodbConnectionFactory([]);
$factory = new MongodbConnectionFactory([
'dsn' => 'mongodb://localhost:27017/db_name',
'dbname' => 'enqueue',
'collection_name' => 'enqueue',
'polling_interval' => '1000',
]);
$psrContext = $factory->createContext();
// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('mongodb:');
<?php
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
/** @var \Enqueue\Mongodb\MongodbDestination $fooTopic */
$message = $psrContext->createMessage('Hello world!');
$psrContext->createProducer()->send($fooTopic, $message);
<?php
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
$message = $psrContext->createMessage('Hello world!');
$psrContext->createProducer()->send($fooQueue, $message);
<?php
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
$fooQueue = $psrContext->createQueue('foo');
$message = $psrContext->createMessage('Hello world!');
$psrContext->createProducer()
->setPriority(5) // the higher priority the sooner a message gets to a consumer
//
->send($fooQueue, $message)
;
<?php
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
$message = $psrContext->createMessage('Hello world!');
$psrContext->createProducer()
->setTimeToLive(60000) // 60 sec
//
->send($fooQueue, $message)
;
<?php
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
// make sure you run "composer require enqueue/amqp-tools".
$message = $psrContext->createMessage('Hello world!');
$psrContext->createProducer()
->setDeliveryDelay(5000) // 5 sec
->send($fooQueue, $message)
;
<?php
/** @var \Enqueue\Mongodb\MongodbContext $psrContext */
/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */
$consumer = $psrContext->createConsumer($fooQueue);
$message = $consumer->receive();
// process a message
$consumer->acknowledge($message);
// $consumer->reject($message);