|
| 1 | +#Introduction |
| 2 | +This package provides an easy way of adding AWS SNS message handling to your Laravel application as a REST endpoint. The package can automatically confirm subscription requests and dispatches events when a message is received. |
| 3 | + |
| 4 | +##How to create an SNS topic |
| 5 | +[Instructions for creating an SNS topic in AWS can be found here](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html) |
| 6 | + |
| 7 | +##How to add SNS Handler to your Application |
| 8 | +**Note: This package currently requires Laravel >= 8.0. Laravel 7 is no longer supported due to security concerns.** |
| 9 | + |
| 10 | +Use composer to require the package: |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require nipwaayoni/laravel-aws-sns |
| 14 | +``` |
| 15 | + |
| 16 | +##Handling events |
| 17 | +Please review the [Laravel documentation on Events.](https://laravel.com/docs/8.x/events) You will need to write a listener to handle the events associated with the ARNs you register. Your listener class must handle the `SnsMessageReceived` event type, which is provided with this package. It should then be registered in the `EventServiceProvider` as mentioned in the Events documentation: |
| 18 | +```php |
| 19 | +use Nipwaayoni\SnsHandler\Events\SnsMessageReceived; |
| 20 | +use App\Listeners\MySnsMessageHandler; |
| 21 | + |
| 22 | +/** |
| 23 | +* The event listener mappings for the application. |
| 24 | +* |
| 25 | +* @var array |
| 26 | + */ |
| 27 | + protected $listen = [ |
| 28 | + SnsMessageReceived::class => [ |
| 29 | + MySnsMessageHandler::class, |
| 30 | + ], |
| 31 | + ]; |
| 32 | + In your listener, the handle method will receive an SnsMessageReceived object which can be used to access the SnsMessage. |
| 33 | + |
| 34 | +public function handle(SnsMessageReceived $event) |
| 35 | +{ |
| 36 | +$message = $event->message(); |
| 37 | + |
| 38 | + // do stuff with message |
| 39 | + $content = $message->content(); |
| 40 | +} |
| 41 | +``` |
| 42 | +The message content will always be a string. You are responsible for any deserialization or other steps required to interpret the message content. |
| 43 | + |
| 44 | +##Write a feature test to test the expected SNS feature |
| 45 | +The ReceivesSnsMessagesTrait facilitates testing. Use the trait in your test |
| 46 | + |
| 47 | +Sample test: |
| 48 | +```php |
| 49 | +public function testSnsRequestMapping(): void |
| 50 | +{ |
| 51 | +Bus::fake(); |
| 52 | +$data = "Bob"; |
| 53 | +$this->sendSnsMessage($data); |
| 54 | + |
| 55 | + Bus::assertDispatched(SayHelloJob::class); |
| 56 | + |
| 57 | + } |
| 58 | +``` |
| 59 | +Disable message signature validation when sending events from sources other than SNS (and during feature tests). Add to .env: |
| 60 | +``` |
| 61 | +VALIDATE_SNS_MESSAGES=false |
| 62 | +``` |
| 63 | + |
| 64 | +##How do I send SNS messages to my app? |
| 65 | +The SNS message route responds to POST requests and expects content consistent with an SNS message from AWS. |
| 66 | + |
| 67 | +The message content of an SNS message must be provided as a string value. If your payload is something other than a simple string (e.g. an array or some other sort of object), you will need to serialize your data before sending it using something like json_encode(). |
| 68 | + |
| 69 | +###POSTing directly to your app |
| 70 | +[Amazon has an example of a POST request available here.](https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html) |
| 71 | +You can add your serialized message in the message field. Note that manually POSTed messages cannot be validated (See above to disable message validation). We recommend only attempting this during testing, never in your production environment. |
| 72 | + |
| 73 | + |
| 74 | +##How to subscribe your endpoint in AWS |
| 75 | +[Follow these instructions to subscribe your endpoint using HTTPS](https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html) |
| 76 | + |
| 77 | +**Note: You will only be able to subscribe your endpoint if it can be reached from the AWS SNS service** |
| 78 | + |
0 commit comments