PHP code example of iamfarhad / laravel-rabbitmq

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

    

iamfarhad / laravel-rabbitmq example snippets


$app->register(iamfarhad\LaravelRabbitMQ\LaravelRabbitQueueServiceProvider::class);

'connections' => [
    // ... other connections
    
    'rabbitmq' => [
        'driver' => 'rabbitmq',
        'queue'  => env('RABBITMQ_QUEUE', 'default'),

        'hosts' => [
            'host'      => env('RABBITMQ_HOST', '127.0.0.1'),
            'port'      => env('RABBITMQ_PORT', 5672),
            'user'      => env('RABBITMQ_USER', 'guest'),
            'password'  => env('RABBITMQ_PASSWORD', 'guest'),
            'vhost'     => env('RABBITMQ_VHOST', '/'),
            'lazy'      => env('RABBITMQ_LAZY_CONNECTION', true),
            'keepalive' => env('RABBITMQ_KEEPALIVE_CONNECTION', false),
            'heartbeat' => env('RABBITMQ_HEARTBEAT_CONNECTION', 0),
            'secure'    => env('RABBITMQ_SECURE', false),
        ],

        'options' => [
            'ssl_options' => [
                'cafile'      => env('RABBITMQ_SSL_CAFILE', null),
                'local_cert'  => env('RABBITMQ_SSL_LOCALCERT', null),
                'local_key'   => env('RABBITMQ_SSL_LOCALKEY', null),
                'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                'passphrase'  => env('RABBITMQ_SSL_PASSPHRASE', null),
            ],
            'queue' => [
                'job' => \iamfarhad\LaravelRabbitMQ\Jobs\RabbitMQJob::class,
                'qos' => [
                    'prefetch_size'  => 0,
                    'prefetch_count' => 10,
                    'global'         => false
                ]
            ],
        ],
    ],
]

// Dispatch a job to the default queue
dispatch(new ProcessPodcast($podcast));

// Dispatch a job to a specific queue
dispatch(new ProcessPodcast($podcast))->onQueue('podcasts');

// Dispatch a job with delay
dispatch(new ProcessPodcast($podcast))->delay(now()->addMinutes(10));

namespace App\Jobs;

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

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

    public function __construct(private $podcast)
    {
        // Specify a custom queue
        $this->onQueue('podcasts');
    }

    public function handle()
    {
        // Process the podcast...
    }
}

$job = (new ProcessPodcast($podcast))->onQueue('podcasts');
dispatch($job->withProperty('priority', 10));

'queue' => [
    'qos' => [
        'prefetch_size'  => 0,    // No specific size limit
        'prefetch_count' => 10,   // Process 10 messages at a time
        'global'         => false // Apply per consumer, not channel
    ]
]

'secure' => true,
'ssl_options' => [
    'cafile'      => '/path/to/ca.pem',
    'local_cert'  => '/path/to/cert.pem',
    'local_key'   => '/path/to/key.pem',
    'verify_peer' => true,
    'passphrase'  => 'certificate-passphrase',
],

// In your job class
public function failed(Throwable $exception)
{
    // Handle failed job
}

// Custom retry delay
public function retryAfter()
{
    return 30; // seconds
}
bash
php artisan queue:work rabbitmq --queue=default
bash
php artisan rabbitmq:consume --queue=default
bash
php artisan rabbitmq:consume [options]
bash
php artisan rabbitmq:consume --queue=default --num-processes=4