Skip to content

palpalani/laravel-sqs-queue-json-reader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

618e81d · Jan 28, 2021

History

45 Commits
Jan 13, 2021
Jan 21, 2021
Jan 28, 2021
Jan 13, 2021
Jan 13, 2021
Jan 13, 2021
Jan 13, 2021
Jan 13, 2021
Jan 17, 2021
Jan 13, 2021
Jan 17, 2021
Jan 25, 2021
Jan 13, 2021
Jan 13, 2021

Repository files navigation

Custom SQS queue reader for Laravel

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Custom SQS queue reader for Laravel that supports plain JSON payloads. Laravel expects SQS messages to be generated in a specific format that includes job handler class and a serialized job.

But in certain cases you may want to parse messages from 3rd party applications, custom JSON messages and so on.

Installation

You can install the package via composer:

composer require palpalani/laravel-sqs-queue-json-reader

You can publish the config file with:

php artisan vendor:publish --provider="palPalani\SqsQueueReader\SqsQueueReaderServiceProvider" --tag="config"

This is the contents of the published config file:

return [

    /**
     * Separate queue handle with corresponding queue name as key.
     */
    'handlers' => [
        //'stripe-webhooks' => App\Jobs\StripeHandler::class,
        //'mailgun-webhooks' => App\Jobs\MailgunHandler::class,
        //'shopify-webhooks' => App\Jobs\ShopifyHandler::class,
    ],

    'default-handler' => App\Jobs\SqsHandler::class
];

If queue is not found in 'handlers' array, SQS payload is passed to default handler.

Add sqs-json connection to your config/queue.php, Ex:

    [
        // Add new SQS connection
        'sqs-json' => [
            'driver' => 'sqs-json',
            'key'    => env('AWS_ACCESS_KEY_ID', ''),
            'secret' => env('AWS_SECRET_ACCESS_KEY', ''),
            'prefix' => env('SQS_PREFIX', 'https://sqs.us-west-2.amazonaws.com/1234567890'),
            'queue'  => env('SQS_QUEUE', 'stripe-webhooks'),
            'region' => env('AWS_DEFAULT_REGION', 'us-west-2'),
        ],
    ]

In your .env file, choose sqs-json as your new default queue driver:

QUEUE_DRIVER=sqs-json

Dispatching to SQS

If you plan to push plain messages from Laravel, you can rely on DispatcherJob:

use palPalani\SqsQueueReader\Jobs\DispatcherJob;

class ExampleController extends Controller
{
    public function index()
    {
        // Create a PHP object
        $object = [
            'music' => 'Sample SQS message',
            'time' => time()
        ];

        // Pass it to dispatcher job
        $job = new DispatcherJob($object);

        // Dispatch the job as you normally would
        // By default, your data will be encapsulated in 'data' and 'job' field will be added
        $this->dispatch($job);

        // If you wish to submit a true plain JSON, add setPlain()
        $this->dispatch($job->setPlain());
    }
}

Above code will push the following JSON object to SQS queue:

{"job":"App\\Jobs\\SqsHandler@handle","data":{"music":"Sample SQS message","time":1464511672}}

'job' field is not used, actually. It's just kept for compatibility with Laravel Framework.

Receiving from SQS

If a 3rd-party application or API Gateway to SQS implementation is creating custom-format JSON messages, just add a handler in the config file and implement a handler class as follows:

use Illuminate\Contracts\Queue\Job as LaravelJob;

class SqsHandlerJob extends Job
{
    /**
     * @var null|array $data
     */
    protected $data;

    /**
     * @param LaravelJob $job
     * @param null|array $data
     */
    public function handle(LaravelJob $job, ?array $data): void
    {
        // This is incoming JSON payload, already decoded to an array
        var_dump($data);

        // Raw JSON payload from SQS, if necessary
        var_dump($job->getRawBody());
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.