PHP code example of jeylabs / laravel-sns-sqs-sub-pub

1. Go to this page and download the library: Download jeylabs/laravel-sns-sqs-sub-pub library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jeylabs / laravel-sns-sqs-sub-pub example snippets



return [
    'default_topic' => 'SampleTopic',
    'default_auth_driver' => null,
    'map' => [
        \App\Jobs\TestSQSJob::class => 'SampleTopic',
    ],
    'published_attributes' => [
        'id',
        'created_at',
        'updated_at'
    ],
    'sns' => [
        'key' => env('SNS_SQS_PUB_SUB_AWS_ACCESS_KEY'),
        'secret' => env('SNS_SQS_PUB_SUB_AWS_SECRET_ACCESS_KEY'),
        'region' => env('SNS_SQS_PUB_SUB_AWS_DEFAULT_REGION', 'us-east-1'),
    ]
];



return [
    'connections' => [
      'sns-sqs-sub-pub' => [
          'driver' => 'sns-sqs-sub-pub',
          'key' => env('SNS_SQS_PUB_SUB_AWS_ACCESS_KEY'),
          'secret' => env('SNS_SQS_PUB_SUB_AWS_SECRET_ACCESS_KEY'),
          'prefix' => env('SNS_SQS_PUB_SUB_SQS_PREFIX', 'https://sqs.ap-southeast-1.amazonaws.com/your-account-id'),
          'queue' => env('SNS_SQS_PUB_SUB_QUEUE_URL', 'your-queue-name'),
          'region' => env('SNS_SQS_PUB_SUB_AWS_DEFAULT_REGION', 'us-east-1'),
      ],
    ],
];


use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class TestSQSJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $passedInData;

    /**
     * Create a new job instance.
     *
     * @param array $data
     */
    public function __construct(array $data)
    {
        // $data is array containing the msg content from SQS
        $this->passedInData = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info(json_encode($this->passedInData));
        // Check laravel.log, it should now contain msg string.
    }
}



namespace App;

use Illuminate\Database\Eloquent\Model;
use Jeylabs\SnsSqsPubSub\Traits\SNSPublisher;

class Post extends Model
{
    use SNSPublisher;

    /**
     * @var array
     * Optional (default value is [] )
     * Witch are the attributes should only from SNS message
     */
    static $publishedAttributes = [
        'id',
        'created_at',
        'updated_at'
    ];

    /**
     * @var array
     * Optional  (default value is [created','updated','deleted','restored'] )
     * Witch events should send to SNS
     */
    static $publishEvents = ['created', 'updated'];

    /**
     * @var string
     * Optional (default value is load from config )
     * Publish SNS topic
     */
    static $publishTopic = 'SampleTopic';
    /**
     * Or
     * static $publishTopic = [
     *  'created' => 'SampleTopic'
     * ];
     */
}
bash
php artisan vendor:publish --provider="Jeylabs\SnsSqsPubSub\SnsSqsPubSubServiceProvider" --tag="config"