PHP code example of lostlink / laravel-messenger

1. Go to this page and download the library: Download lostlink/laravel-messenger 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/ */

    

lostlink / laravel-messenger example snippets


return [

    'default' => env('LARAVEL_MESSENGER_DRIVER', 'log'),

    'drivers' => [

        'log' => [
            'class' => Lostlink\Messenger\Drivers\Log::class,
            'rate_limit' => [
                'enabled' => env('LARAVEL_MESSENGER_LOG_RATE_LIMIT_ENABLED', false),
                'max_attempts' => env('LARAVEL_MESSENGER_LOG_RATE_LIMIT_MAX_ATTEMPTS', 10),
                'decay_seconds' => env('LARAVEL_MESSENGER_LOG_RATE_LIMIT_DECAY_SECONDS', 60),
            ],
        ],

        'kinesis' => [
            'class' => Lostlink\Messenger\Drivers\Kinesis::class,
            'name' => env('LARAVEL_MESSENGER_KINESIS_STREAM_NAME'),
            'region' => env('LARAVEL_MESSENGER_KINESIS_STREAM_AWS_REGION', env('AWS_DEFAULT_REGION')),
            'aws_key' => env('LARAVEL_MESSENGER_KINESIS_STREAM_AWS_KEY', env('AWS_ACCESS_KEY_ID')),
            'aws_secret_key' => env('LARAVEL_MESSENGER_KINESIS_STREAM_AWS_SECRET_KEY', env('AWS_SECRET_ACCESS_KEY')),
            'rate_limit' => [
                'enabled' => env('LARAVEL_MESSENGER_KINESIS_RATE_LIMIT_ENABLED', false),
                'max_attempts' => env('LARAVEL_MESSENGER_KINESIS_RATE_LIMIT_MAX_ATTEMPTS', 10),
                'decay_seconds' => env('LARAVEL_MESSENGER_KINESIS_RATE_LIMIT_DECAY_SECONDS', 60),
            ],
        ],

        'tinybird' => [
            'class' => Lostlink\Messenger\Drivers\Tinybird::class,
            'name' => env('LARAVEL_MESSENGER_TINYBIRD_DATA_SOURCE_NAME'),
            'token' => env('LARAVEL_MESSENGER_TINYBIRD_TOKEN'),
            'endpoint' => env('LARAVEL_MESSENGER_TINYBIRD_ENDPOINT', 'https://api.us-east.aws.tinybird.co/v0/events'),
            'rate_limit' => [
                'enabled' => env('LARAVEL_MESSENGER_TINYBIRD_RATE_LIMIT_ENABLED', false),
                'max_attempts' => env('LARAVEL_MESSENGER_TINYBIRD_RATE_LIMIT_MAX_ATTEMPTS', 40), // Free tier limit
                'decay_seconds' => env('LARAVEL_MESSENGER_TINYBIRD_RATE_LIMIT_DECAY_SECONDS', 3600), // Free tier limit
            ],
        ],

    ],

];

use \Lostlink\Messenger\Messenger;

Messenger::send([
   "timestamp" => "2022-10-27T11:43:02.099Z", 
   "transaction_id" => "8d1e1533-6071-4b10-9cda-b8429c1c7a67", 
   "name" => "Bobby Drake", 
   "email" => "[email protected]", 
   "age" => 42, 
   "passport_number" => 3847665, 
   "flight_from" => "Barcelona", 
   "flight_to" => "London", 
   "extra_bags" => 1, 
   "flight_class" => "economy", 
   "priority_boarding" => false, 
   "meal_choice" => "vegetarian", 
   "seat_number" => "15D", 
   "airline" => "Red Balloon" 
]);

use \Lostlink\Messenger\Messenger;

Messenger::send([
   "timestamp" => "2022-10-27T11:43:02.099Z", 
   "transaction_id" => "8d1e1533-6071-4b10-9cda-b8429c1c7a67", 
   "name" => "Bobby Drake", 
   "email" => "[email protected]", 
   "age" => 42, 
   "passport_number" => 3847665, 
   "flight_from" => "Barcelona", 
   "flight_to" => "London", 
   "extra_bags" => 1, 
   "flight_class" => "economy", 
   "priority_boarding" => false, 
   "meal_choice" => "vegetarian", 
   "seat_number" => "15D", 
   "airline" => "Red Balloon" 
])
->driver('tinybird') // Change the driver to "tinybird" for this message
->config([
    'name' => 'example_events', // Change the data source name to "example_events" for this message
    'token' => 'your_token', // Change the token for this message
    'endpoint' => 'https://api.us-east.aws.tinybird.co/v0/events', // Change the endpoint for this message
    'rate_limit' => [
        'enabled' => true, // Enable rate limiting for this message to use Tinybird Free Tier @ 1000/day
        'max_attempts' => 40, // Set the maximum number of attempts before rate limiting is enforced
        'decay_seconds' => 3600, // Set the number of seconds before the rate limit resets
    ],
]);

   
   return [
        ...   

       'drivers' => [
   
            ...

           'mycustomdriver' => [
               'class' => \App\Messenger\Drivers\MyCustomDriver::class,
               'name' => env('MY_CUSTOM_DRIVER_NAME'),
               'token' => env('MY_CUSTOM_DRIVER_TOKEN'),
               'endpoint' => env('MY_CUSTOM_DRIVER_ENDPOINT'),
               'rate_limit' => [
                   'enabled' => env('MY_CUSTOM_DRIVER_RATE_LIMIT_ENABLED', false),
                   'max_attempts' => env('MY_CUSTOM_DRIVER_RATE_LIMIT_MAX_ATTEMPTS', 10),
                   'decay_seconds' => env('MY_CUSTOM_DRIVER_RATE_LIMIT_DECAY_SECONDS', 60),
               ],
           ],
   
       ],
   
       ...

   ];
   

   class MyCustomDriver extends Driver
   {
       public function handle(): void
       {
           $response = Http::withToken($this->message->token ?? $this->message->config->get('token'))
               ->acceptJson()
               ->withQueryParameters([
                   'name' => $this->message->stream ?? $this->message->config->get('name'),
               ])
               ->post(
                   $this->message->endpoint ?? $this->message->config->get('endpoint'),
                   $this->message->body
               );
   
           $response->throw();
       }
   }
   
bash
   php artisan vendor:publish --tag=messenger-config
   
bash
   php artisan vendor:publish --tag=messenger-config