pawprintdigital/laravel-queue-raw-sqs
Installation
composer require pawprintdigital/laravel-queue-raw-sqs
Publish the config (if needed):
php artisan vendor:publish --provider="PawprintDigital\LaravelQueueRawSqs\RawSqsServiceProvider"
Configure Queue Connection
Add a new connection in config/queue.php:
'connections' => [
'raw-sqs' => [
'driver' => 'raw-sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'queue' => env('RAW_SQS_QUEUE_URL'),
'visibility_timeout' => 60,
],
],
First Use Case: Dispatch a Raw SQS Job
use PawprintDigital\LaravelQueueRawSqs\Jobs\RawSqsJob;
RawSqsJob::dispatch('my-queue-name', [
'key' => 'value',
'data' => ['nested' => 'payload']
]);
Consuming Raw Messages
Register a listener in app/Providers/EventServiceProvider.php:
protected $listen = [
'PawprintDigital\LaravelQueueRawSqs\Events\RawSqsMessageReceived' => [
'App\Listeners\HandleRawSqsMessage',
],
];
Dispatching Messages
Use RawSqsJob for structured payloads:
RawSqsJob::dispatch('notifications', [
'event' => 'user.created',
'user_id' => 123,
]);
Handling Incoming Messages
Implement a listener for RawSqsMessageReceived:
public function handle(RawSqsMessageReceived $event) {
$message = $event->message;
$queue = $event->queueName;
if ($queue === 'notifications') {
$this->processNotification($message);
}
}
Batch Processing
For high-volume queues, use RawSqsJob::dispatchMany():
RawSqsJob::dispatchMany('batch-jobs', [
['id' => 1, 'type' => 'process'],
['id' => 2, 'type' => 'archive'],
]);
Integration with Existing Queues Combine with Laravel’s default queues:
// Dispatch to SQS via raw-sqs connection
dispatch(new ProcessOrder)->onQueue('raw-sqs');
// Dispatch to default queue
dispatch(new SendEmail)->onQueue('default');
Custom Message Handlers
Override RawSqsMessageReceived logic in a service provider:
public function boot() {
RawSqsMessageReceived::extend(function ($message, $queue) {
return new CustomMessageHandler($message, $queue);
});
}
Middleware for Raw SQS Apply middleware to raw SQS jobs:
RawSqsJob::dispatch('audit-log', $data)
->onConnection('raw-sqs')
->after(function ($job) {
Log::info('Raw SQS job dispatched', $job->payload);
});
Visibility Timeout
'visibility_timeout' => 300, // 5 minutes
VisibilityTimeout errors in CloudWatch for stuck messages.Message Size Limits
json_encode with JSON_THROW_ON_ERROR).Duplicate Processing
if (!ProcessedMessage::exists($message['id'])) {
ProcessedMessage::create(['id' => $message['id']]);
// Process message
}
Connection Configuration
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION are set in .env.Log Raw Messages Add a listener to log incoming messages:
public function handle(RawSqsMessageReceived $event) {
\Log::debug('Raw SQS message', [
'queue' => $event->queueName,
'message' => $event->message,
]);
}
Check SQS Metrics
Monitor ApproximateNumberOfMessagesVisible in AWS Console to debug queue backlogs.
Test Locally
Use localstack to mock SQS:
docker run -it -p 4566:4566 localstack/localstack
Update config:
'url' => 'http://localhost:4566/000000000000/my-queue',
Custom Queue Naming Override queue name resolution:
RawSqsJob::dispatchWithQueueName('custom-prefix-' . $queue, $payload);
Message Transformation Modify payloads before dispatch:
RawSqsJob::dispatch('transformed', $data)
->transform(function ($payload) {
return array_merge($payload, ['timestamp' => now()->toIso8601String()]);
});
Error Handling Catch SQS-specific exceptions:
try {
RawSqsJob::dispatch('critical', $data);
} catch (\Aws\Sqs\Exception\SqsException $e) {
\Log::error('SQS dispatch failed', ['error' => $e->getMessage()]);
// Retry or fallback logic
}
Delayed Messages Use SQS delay queues (configure via AWS Console) or implement a wrapper:
RawSqsJob::dispatch('delayed', $data)->delay(seconds: 300);
How can I help you explore Laravel packages today?