@@ -15,30 +15,112 @@ You can install the package via composer:
1515composer require palpalani/laravel-sqs-queue-json-reader
1616```
1717
18- You can publish and run the migrations with:
19-
20- ``` bash
21- php artisan vendor:publish --provider=" Palpalani\SqsQueueReader\SqsQueueReaderServiceProvider" --tag=" migrations"
22- php artisan migrate
23- ```
24-
2518You can publish the config file with:
2619``` bash
27- php artisan vendor:publish --provider=" Palpalani \SqsQueueReader\SqsQueueReaderServiceProvider" --tag=" config"
20+ php artisan vendor:publish --provider=" palPalani \SqsQueueReader\SqsQueueReaderServiceProvider" --tag=" config"
2821```
2922
3023This is the contents of the published config file:
3124
3225``` php
3326return [
27+
28+ /**
29+ * Separate queue handle with corresponding queue name as key.
30+ */
31+ 'handlers' => [
32+ //'stripe-webhooks' => App\Jobs\SqsHandler::class,
33+ ],
34+
35+ 'default-handler' => App\Jobs\SqsHandler::class
3436];
3537```
3638
37- ## Usage
39+ If queue is not found in 'handlers' array, SQS payload is passed to default handler.
40+
41+ Add sqs-json connection to your config/queue.php, eg:
42+
43+ ``` php
44+ [
45+ // Add connection
46+ 'sqs-json' => [
47+ 'driver' => 'sqs-json',
48+ 'key' => env('AWS_KEY', ''),
49+ 'secret' => env('AWS_SECRET', ''),
50+ 'prefix' => 'https://sqs.us-west-2.amazonaws.com/3242342351/',
51+ 'queue' => 'stripe-webhooks',
52+ 'region' => 'ap-southeast-2',
53+ ],
54+ ]
55+ ```
56+
57+ In your .env file, choose sqs-plain as your new default queue driver:
58+
59+ ```
60+ QUEUE_DRIVER=sqs-json
61+ ```
62+
63+ Dispatching to SQS
64+
65+ If you plan to push plain messages from Laravel or Lumen, you can rely on DispatcherJob:
66+
67+ ``` php
68+ use palPalani\SqsQueueReader\Jobs\DispatcherJob;
69+
70+ class ExampleController extends Controller
71+ {
72+ public function index()
73+ {
74+ // Create a PHP object
75+ $object = [
76+ 'music' => 'Sample message',
77+ 'time' => time()
78+ ];
79+
80+ // Pass it to dispatcher job
81+ $job = new DispatcherJob($object);
82+
83+ // Dispatch the job as you normally would
84+ // By default, your data will be encapsulated in 'data' and 'job' field will be added
85+ $this->dispatch($job);
86+
87+ // If you wish to submit a true plain JSON, add setPlain()
88+ $this->dispatch($job->setPlain());
89+ }
90+ }
91+ ```
92+ This will push the following JSON object to SQS:
93+
94+ ``` json
95+ {"job" :" App\\ Jobs\\ SqsHandler@handle" ,"data" :{"music" :" Sample message" ,"time" :1464411642 }}
96+ ```
97+
98+ 'job' field is not used, actually. It's just kept for compatibility.
99+
100+ Receiving from SQS
101+ If a third-party application is creating custom-format JSON messages, just add a
102+ handler in the config file and implement a handler class as follows:
38103
39104``` php
40- $laravel-sqs-queue-json-reader = new Palpalani\SqsQueueReader();
41- echo $laravel-sqs-queue-json-reader->echoPhrase('Hello, Palpalani!');
105+ use Illuminate\Contracts\Queue\Job as LaravelJob;
106+
107+ class HandlerJob extends Job
108+ {
109+ protected $data;
110+
111+ /**
112+ * @param LaravelJob $job
113+ * @param array $data
114+ */
115+ public function handle(LaravelJob $job, array $data)
116+ {
117+ // This is incoming JSON payload, already decoded to an array
118+ var_dump($data);
119+
120+ // Raw JSON payload from SQS, if necessary
121+ var_dump($job->getRawBody());
122+ }
123+ }
42124```
43125
44126## Testing
0 commit comments