enqueue/sns
Amazon SNS transport for Enqueue/Queue Interop. Send and consume messages via AWS SNS using a standards-based PHP queue abstraction. Part of the Enqueue ecosystem with docs, CI, and Packagist distribution.
Installation
composer require enqueue/sns
Requires php-enqueue/amqp-ext or php-enqueue/amqp-lib for AWS SDK compatibility.
First Use Case: Sending a Message
use Enqueue\Sns\SnsConnectionFactory;
use Enqueue\Sns\SnsContext;
$factory = new SnsConnectionFactory(
new \Aws\Sns\SnsClient([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'YOUR_AWS_KEY',
'secret' => 'YOUR_AWS_SECRET',
],
])
);
$connection = $factory->createContext();
$producer = $connection->createProducer();
$producer->send(new \Enqueue\Message\Message('Hello SNS!', ['topic' => 'my-topic']));
Key Files to Review
src/SnsConnectionFactory.php (Connection setup)src/SnsContext.php (Context management)src/SnsProducer.php (Message publishing)src/SnsConsumer.php (Message consumption)Producer Workflow
$producer = $connection->createProducer();
$producer->send(new Message('Data', [
'topic' => 'my-topic',
'attributes' => ['priority' => 'high']
]));
Consumer Workflow (Polling)
$consumer = $connection->createConsumer('my-topic');
$consumer->setMessageHandler(function ($message) {
// Process message
$message->ack();
});
$consumer->consume();
Batch Publishing
$batchProducer = $connection->createBatchProducer();
$batchProducer->sendBatch([
new Message('Batch 1'),
new Message('Batch 2'),
]);
Laravel Queue Integration
Add to config/queue.php:
'sns' => [
'driver' => 'sns',
'connection' => 'sns',
'queue' => 'default',
'options' => [
'region' => env('AWS_REGION'),
'credentials' => [
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
],
],
],
Then bind in AppServiceProvider:
Queue::extend('sns', function ($app) {
$factory = new SnsConnectionFactory(
new \Aws\Sns\SnsClient($app['config']['queue.sns.options'])
);
return $factory->createContext();
});
Retry Logic
Use Enqueue\Retry\RetryContext for transient failures:
$retryContext = new RetryContext($connection);
$producer = $retryContext->createProducer();
Message Serialization
Customize serialization via MessageConverter:
$converter = new \Enqueue\MessageConverter\JsonMessageConverter();
$producer = $connection->createProducer($converter);
AWS Credentials
SNS:Publish and SNS:Subscribe permissions.Message Size Limits
Consumer Lag
setDelay() to control polling intervals:
$consumer->setDelay(1000); // 1 second delay
Message Visibility
Topic vs. Queue Confusion
FIFO topics for ordered delivery if needed.Enable AWS SDK Debugging
$client = new \Aws\Sns\SnsClient([
'debug' => true,
'logger' => new \Aws\Logger\StreamLogger('php://stdout'),
]);
Check SNS Metrics
Monitor NumberOfMessagesPublished and NumberOfNotificationsDelivered in AWS CloudWatch.
Custom Message Attributes
Extend SnsMessage to add metadata:
class CustomSnsMessage extends \Enqueue\Sns\SnsMessage {
public function __construct($body, array $headers = [], array $attributes = []) {
parent::__construct($body, $headers, array_merge($attributes, ['custom' => 'value']));
}
}
Middleware for Pre/Post Processing
Use Enqueue\Client\Middleware to intercept messages:
$producer = $connection->createProducer();
$producer->withMiddleware(new class implements Middleware {
public function handle($message, callable $next) {
// Pre-process
$result = $next($message);
// Post-process
return $result;
}
});
Dead Letter Queues (DLQ) Implement a fallback topic for failed messages:
$producer->send(new Message('Data', [
'topic' => 'my-topic',
'attributes' => ['dlq-topic' => 'my-dlq-topic']
]));
Region-Specific Endpoints Ensure the AWS region matches your SNS topic’s region. Cross-region publishing requires additional configuration.
HTTPS vs. HTTP The AWS SDK defaults to HTTPS. Force HTTP only if necessary (e.g., internal networks):
'scheme' => 'http',
Message Deduplication
Use MessageDeduplicationId for idempotency:
$producer->send(new Message('Data', [
'topic' => 'my-topic',
'attributes' => ['MessageDeduplicationId' => uniqid()]
]));
How can I help you explore Laravel packages today?