|
| 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 >= 7.0.** |
| 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 | + |
| 66 | +Instructions pending. |
| 67 | + |
| 68 | +##How to subscribe your endpoint in AWS |
| 69 | +[Follow these instructions to subscribe your endpoint using HTTPS](https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html) |
| 70 | + |
| 71 | +**Note: You will only be able to subscribe your endpoint if it can be reached from the AWS SNS service** |
| 72 | + |
0 commit comments