PHP code example of sysmatter / laravel-google-pubsub
1. Go to this page and download the library: Download sysmatter/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/ */
sysmatter / laravel-google-pubsub example snippets
// Dispatch jobs as normal
ProcessPodcast::dispatch($podcast);
// Dispatch to specific queue (Pub/Sub topic)
ProcessPodcast::dispatch($podcast)->onQueue('audio-processing');
// Your Go microservices can subscribe to the same topic
// Subscription name: audio-processing-go-service
use SysMatter\GooglePubSub\Facades\PubSub;
// Publish directly to a topic
PubSub::publish('orders', [
'order_id' => 123,
'total' => 99.99,
'customer_id' => 456
]);
// With attributes and ordering
PubSub::publish('orders', $data, [
'priority' => 'high',
'source' => 'api'
], [
'ordering_key' => 'customer-456'
]);
use SysMatter\GooglePubSub\Attributes\PublishTo;
use SysMatter\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,
];
}
}
// This event automatically publishes to the 'orders' topic
event(new OrderPlaced($order));
use SysMatter\GooglePubSub\Facades\PubSub;
// Create a subscriber
$subscriber = PubSub::subscribe('orders-processor', 'orders');
// Add message handler
$subscriber->handler(function ($data, $message) {
// Process the order
processOrder($data);
});
// Start listening
$subscriber->listen();