layout | title | parent | nav_order |
---|---|---|---|
default |
Amazon SQS |
Transports |
3 |
{% include support.md %}
A transport for Amazon SQS broker. It uses internally official aws sdk library
- Installation
- Create context
- Declare queue
- Send message to queue
- Send delay message
- Consume message
- Purge queue messages
- Queue from another AWS account
$ composer require enqueue/sqs
<?php
use Enqueue\Sqs\SqsConnectionFactory;
$factory = new SqsConnectionFactory([
'key' => 'aKey',
'secret' => 'aSecret',
'region' => 'aRegion',
]);
// same as above but given as DSN string. You may need to url encode secret if it contains special char (like +)
$factory = new SqsConnectionFactory('sqs:?key=aKey&secret=aSecret®ion=aRegion');
$context = $factory->createContext();
// using a pre-configured client
$client = new Aws\Sqs\SqsClient([ /* ... */ ]);
$factory = new SqsConnectionFactory($client);
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$context = (new \Enqueue\ConnectionFactoryFactory())->create('sqs:')->createContext();
Declare queue operation creates a queue on a broker side.
<?php
/** @var \Enqueue\Sqs\SqsContext $context */
$fooQueue = $context->createQueue('foo');
$context->declareQueue($fooQueue);
// to remove queue use deleteQueue method
//$context->deleteQueue($fooQueue);
<?php
/** @var \Enqueue\Sqs\SqsContext $context */
$fooQueue = $context->createQueue('foo');
$message = $context->createMessage('Hello world!');
$context->createProducer()->send($fooQueue, $message);
<?php
/** @var \Enqueue\Sqs\SqsContext $context */
$fooQueue = $context->createQueue('foo');
$message = $context->createMessage('Hello world!');
$context->createProducer()
->setDeliveryDelay(60000) // 60 sec
->send($fooQueue, $message)
;
<?php
/** @var \Enqueue\Sqs\SqsContext $context */
$fooQueue = $context->createQueue('foo');
$consumer = $context->createConsumer($fooQueue);
$message = $consumer->receive();
// process a message
$consumer->acknowledge($message);
// $consumer->reject($message);
<?php
/** @var \Enqueue\Sqs\SqsContext $context */
$fooQueue = $context->createQueue('foo');
$context->purgeQueue($fooQueue);
SQS allows to use queues from another account. You could set it globally for all queues via option queue_owner_aws_account_id
or
per queue using SqsDestination::setQueueOwnerAWSAccountId
method.
<?php
use Enqueue\Sqs\SqsConnectionFactory;
// globally for all queues
$factory = new SqsConnectionFactory('sqs:?queue_owner_aws_account_id=awsAccountId');
$context = (new SqsConnectionFactory('sqs:'))->createContext();
// per queue.
$queue = $context->createQueue('foo');
$queue->setQueueOwnerAWSAccountId('awsAccountId');
Enqueue SQS provides a generic multi-region support. This enables users to specify which AWS Region to send a command to by setting region on SqsDestination. You might need it to access SQS FIFO queue because they are not available for all regions. If not specified the default region is used.
<?php
use Enqueue\Sqs\SqsConnectionFactory;
$context = (new SqsConnectionFactory('sqs:?region=eu-west-2'))->createContext();
$queue = $context->createQueue('foo');
$queue->setRegion('us-west-2');
// the request goes to US West (Oregon) Region
$context->declareQueue($queue);