You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
3
4
-
##How to create an SNS topic
4
+
##How to create an SNS topic
5
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
6
7
-
##How to add SNS Handler to your Application
7
+
##How to add SNS Handler to your Application
8
8
**Note: This package currently requires Laravel >= 8.0. Laravel 7 is no longer supported due to security concerns.**
9
9
10
10
Use composer to require the package:
@@ -13,7 +13,7 @@ Use composer to require the package:
13
13
composer require nipwaayoni/laravel-aws-sns
14
14
```
15
15
16
-
##Handling events
16
+
##Handling events
17
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
18
```php
19
19
use Nipwaayoni\SnsHandler\Events\SnsMessageReceived;
@@ -25,54 +25,53 @@ use App\Listeners\MySnsMessageHandler;
25
25
* @var array
26
26
*/
27
27
protected $listen = [
28
-
SnsMessageReceived::class => [
29
-
MySnsMessageHandler::class,
30
-
],
28
+
SnsMessageReceived::class => [
29
+
MySnsMessageHandler::class,
30
+
],
31
31
];
32
-
In your listener, the handle method will receive an SnsMessageReceived object which can be used to access the SnsMessage.
33
-
32
+
```
33
+
In your listener, the handle method will receive an SnsMessageReceived object which can be used to access the SnsMessage.
34
+
```php
34
35
public function handle(SnsMessageReceived $event)
35
36
{
36
-
$message = $event->message();
37
-
38
-
// do stuff with message
39
-
$content = $message->content();
37
+
$message = $event->message();
38
+
// do stuff with message
39
+
$content = $message->content();
40
40
}
41
41
```
42
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
43
44
-
##Write a feature test to test the expected SNS feature
44
+
##Write a feature test to test the expected SNS feature
45
45
The ReceivesSnsMessagesTrait facilitates testing. Use the trait in your test
46
46
47
47
Sample test:
48
48
```php
49
49
public function testSnsRequestMapping(): void
50
50
{
51
-
Bus::fake();
52
-
$data = "Bob";
53
-
$this->sendSnsMessage($data);
54
-
55
-
Bus::assertDispatched(SayHelloJob::class);
56
-
57
-
}
51
+
Bus::fake();
52
+
$data = "Bob";
53
+
$this->sendSnsMessage($data);
54
+
Bus::assertDispatched(SayHelloJob::class);
55
+
}
58
56
```
59
57
Disable message signature validation when sending events from sources other than SNS (and during feature tests). Add to .env:
60
58
```
61
59
VALIDATE_SNS_MESSAGES=false
62
60
```
63
61
64
-
##How do I send SNS messages to my app?
62
+
##How do I send SNS messages to my app?
65
63
The SNS message route responds to POST requests and expects content consistent with an SNS message from AWS.
66
64
67
65
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
66
69
-
###POSTing directly to your app
67
+
###POSTing directly to your app
70
68
[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
69
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
70
73
71
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)
72
+
## How to subscribe your endpoint in AWS
73
+
This package adds a route to your application for incoming SNS requests. Note that because this is an api route, any API middleware you have applied in your application will affect this route. The route will be `{Your application's base URL}/api/sns/message`.
74
+
[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
75
77
76
**Note: You will only be able to subscribe your endpoint if it can be reached from the AWS SNS service**
0 commit comments