Sns Messages as queues using extended laravel sqs driver.
maxgaurav/laravel-sns-sqs-queue is a Laravel package for sns messages as queues using extended laravel sqs driver..
It currently has 4 GitHub stars and 3.817 downloads on Packagist (latest version v1.1.0).
Install it with composer require maxgaurav/laravel-sns-sqs-queue.
Discover more Laravel packages by maxgaurav
or browse all Laravel packages to compare alternatives.
Last updated
This package allows you to process sns messages posted on various topics subscribed by an SQS to be processed as a job.
The queue also processes standard jobs pushed via laravel.
This package is a great use cases for applications beings deployed to microservices.
Install using composer
composer require maxgaurav/laravel-sns-sqs-queue
The package will automatically register its service provider.
In queue.php add the following driver setup
return [
//...
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'your-queue-name'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'sns-sqs' => [
'driver' => 'sns-sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'your-queue-name'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'sns-config' => [
'topics' => [
'topicName' => \App\Jobs\YourJob::class,
'topic2Name' => \App\Jobs\YourOtherJob::class
//...
],
'default-job' => '', // if you want to override the default job executed for non matching topics
'prefix' => env('SNS_PREFIX', 'arn:aws:sns:us-east-2:123345666:'), // SNS Topic Prefix (must include the last colon)
],
],
Each job created must have two constructor inputs. First input is for the topic name the job is executing for and second is the array of json decoded data sent in the message.
class SampleJob {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var string
*/
public $topic;
/**
* @var array
*/
public $body;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($topic, $body)
{
$this->topic = $topic;
$this->body = $body;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// do your handling
}
}
The package uses a default job instance \MaxGaurav\LaravelSnsSqsQueue\DefaultJob for topics not mapped in the configuration. The job has a default functionality to fail for such topics. This is done to allow the application to tell that topics are not mapped.
The DefaultJob class can be replaced with a custom default job handler using key default-job. Pass your class instance and it would be used instead.
The package can also be used to push normal jobs to SQS queue as done using sqs driver. The queue would check if the job is a sns job then would map to topic name and would call the mapped jobs otherwise would fallback to default SQS driver behavior.