PHP code example of offload-project / laravel-google-pubsub

1. Go to this page and download the library: Download offload-project/laravel-google-pubsub 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/ */

    

offload-project / laravel-google-pubsub example snippets


'connections' => [
    'pubsub' => [
        'driver' => 'pubsub',
        'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'),
        'queue' => env('PUBSUB_DEFAULT_QUEUE', 'default'),
        'auth_method' => env('PUBSUB_AUTH_METHOD', 'application_default'),
        'key_file' => env('GOOGLE_APPLICATION_CREDENTIALS'),
        'auto_create_topics' => true,
        'auto_create_subscriptions' => true,
        'subscription_suffix' => '-laravel',
        'enable_message_ordering' => false,
    ],
],

ProcessPodcast::dispatch($podcast);

// Dispatch to a specific topic
ProcessPodcast::dispatch($podcast)->onQueue('audio-processing');

use OffloadProject\GooglePubSub\Facades\PubSub;

PubSub::publish('orders', [
    'order_id' => 123,
    'total' => 99.99,
    'customer_id' => 456,
]);

// With attributes and an ordering key
PubSub::publish('orders', $data, [
    'priority' => 'high',
    'source' => 'api',
], [
    'ordering_key' => 'customer-456',
]);

use OffloadProject\GooglePubSub\Attributes\PublishTo;
use OffloadProject\GooglePubSub\Contracts\ShouldPublishToPubSub;

#[PublishTo('orders')]
class OrderPlaced implements ShouldPublishToPubSub
{
    public function __construct(public Order $order) {}

    public function pubsubTopic(): string
    {
        return 'orders';
    }

    public function toPubSub(): array
    {
        return [
            'order_id' => $this->order->id,
            'total' => $this->order->total,
            'customer_id' => $this->order->customer_id,
        ];
    }
}

event(new OrderPlaced($order));

use OffloadProject\GooglePubSub\Facades\PubSub;

$subscriber = PubSub::subscribe('orders-processor', 'orders');

$subscriber->handler(function ($data, $message) {
    processOrder($data);
});

$subscriber->listen();
bash
php artisan boost:add-skill offload-project/laravel-google-pubsub