PHPackages                             mamitech/laravel-sqs-subscriber - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. mamitech/laravel-sqs-subscriber

ActiveLibrary[Queues &amp; Workers](/categories/queues)

mamitech/laravel-sqs-subscriber
===============================

Mamikos internal package to utilize laravel queue:work command to listen to Amazon SQS message.

v3.2(9mo ago)013.7k↓20.6%1MITPHPPHP ^7.2|^8.0

Since Apr 1Pushed 9mo ago6 watchersCompare

[ Source](https://github.com/mamitech/laravel-sqs-subscriber)[ Packagist](https://packagist.org/packages/mamitech/laravel-sqs-subscriber)[ RSS](/packages/mamitech-laravel-sqs-subscriber/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (2)Versions (12)Used By (0)

Usage
=====

[](#usage)

1. Add the library into your composer' dependencies

```
composer require mamitech/laravel-sqs-subscriber

```

2. Then, publish the configuration:

```
php artisan vendor:publish --provider=Mamitech\LaravelSqsSubscriber\ServiceProvider

```

at this point you should see a new file named `sqs-topic-map.php` in your `config/` directory.

3. Add these env config in you `.env` file

```
AWS_DEFAULT_REGION=ap-southeast-1
AWS_ACCESS_KEY_ID=your-public-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_SQS_PREFIX=https://sqs.ap-southeast-1.amazonaws.com/your-account-id
AWS_SQS_MAX_RECEIVE_MESSAGE=1
AWS_SQS_DISTRIBUTED_DEFAULT_QUEUE=test-local
AWS_SQS_DISTRIBUTED_USE_TOPIC=true

```

Troubleshoot
------------

[](#troubleshoot)

- If you experience a weirdness that somehow don't see this new file after publishing the config, please re-run `vendor:publish` without parameter:

```
php artisan vendor:publish

```

and select the number with `Provider: Mamitech\LaravelSqsSubscriber\ServiceProvider`. Now you should have `sqs-topic-map.php` in your config. (see [link](https://codeburst.io/if-vendor-publish-doesnt-work-laravel-ca889198f828))

- If you use Lumen, publishing config will fail due to no Laravel-specific helper method, please add new method `config_path()` as helper in your project. (see [link](https://gist.github.com/mabasic/21d13eab12462e596120))

Configuration
=============

[](#configuration)

In mamikos we have a standardization about how a message should be formatted to be put into queue, read: [message format](https://mamikos.atlassian.net/wiki/spaces/MAMIKOS/pages/2056126470/Publish-subscribe+communication+across+services+in+Mamikos+System#Message-Format). By looking into the 'topic' field, we can see what is the message about.

Define the Mapping Between Topic and a Class in Your Code
---------------------------------------------------------

[](#define-the-mapping-between-topic-and-a-class-in-your-code)

In `config/sqs-topic-map.php`, specify the mapping between topics to classes that will handle the message. Your class should have a `handle` method receiving one `message` parameter. `message` would contains either string or array depending on how the string inside the queue is being encoded. If it's a proper json encoded string then it would be an array, otherwise it would be string. For details see example below.

Add a New Connector Using `sqs-distributed` Driver into Your `config/queue.php`
-------------------------------------------------------------------------------

[](#add-a-new-connector-using-sqs-distributed-driver-into-your-configqueuephp)

Now you will need a new queue connection using the new driver called `sqs-distributed` that is provided by this library. Please note that we are using `sqs-distributed` terms for the new driver here to get a sense that the message is published and consumed by different services instead of how Laravel original queue works - Laravel original queue can only works if the publisher and subscriber comes from the same service.

See example on the following section.

Example
=======

[](#example)

Suppose that you have a queue in Amazon SQS named `user-registration`. An example of the message inside the queue looks like this:

```
{
  "topic": "user-verified",
  "message": {
    "user": {
      "email": "walker@gmail.com"
    }
  }
}

```

Here's what you need to do.

Add New Connection in Your `config/queue.php`
---------------------------------------------

[](#add-new-connection-in-your-configqueuephp)

Add the new connection inside your queue config using `sqs-distributed` as driver value:

```
