PHP code example of gdg-tangier / cloud-pubsub

1. Go to this page and download the library: Download gdg-tangier/cloud-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/ */

    

gdg-tangier / cloud-pubsub example snippets


'pubsub' => [
      'driver' => 'pubsub',
      'queue'  => env('SUBSCRIPTION'),
      'credentials' => [
          'keyFilePath' => storage_path(env('PUBSUB_CLIENT_KEY')), // credentials file path '.json'
          'projectId'   => env('GCP_PROJECT_ID'),
      ],
  ],



return [
    /*
     * GCP Credentials.
     */
    'credentials' => [
        'keyFilePath' => storage_path(env('PUBSUB_CLIENT_KEY', 'client')),
        'projectId'   => env('GCP_PROJECT_ID'),
    ],

    /*
     * Here where you map events name with Google Pub/Sub topics.
     *
     * Means, map each event name to specific Google Pub/Sub topic.
     */
    'events'      => [
        'event_name' => '__YOUR_TOPIC_NAME__',
    ],

    /*
     * Here where you can tie the subscription classes (jobs) to topics.
     *
     * Means, map each subscription job to a specific Google pubsub topic.
     * The subscription job is responsible for handling the incoming data
     * from a Google Pub/Sub topic.
     */
    'subscriptions'        => [
        \App\PubSub\DummyJob::class => '__YOUR_TOPIC_NAME__',
    ],
];



namespace App\Subscribers;

use GDGTangier\PubSub\Subscriber\SubscriberJob;
use GDGTangier\PubSub\Subscriber\Traits\JobHandler;

class UserUpdated
{
    use JobHandler;

    /**
     * @var mixed
     */
    public $payload;

    /**
     * @var \GDGTangier\PubSub\Subscriber\SubscriberJob
     */
    public $job;

    /**
     * UserUpdated constructor.
     *
     * @param \GDGTangier\PubSub\Subscriber\SubscriberJob $job
     * @param $payload
     */
    public function __construct(SubscriberJob $job, $payload)
    {
        $this->job = $job;
        $this->payload = $payload;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 
    }
}

use GDGTangier\PubSub\Publisher\Facades\PublisherFacade;

PublisherFacade::publish('MyData', 'event_name');

$publisher = app('gcloud.publisher.connection');

$publisher->publish('MyData', 'event_name');