jag / laravel-broadcaster-google-pubsub
Google PubSub on Laravel Broadcaster
Requires
- php: ^7.1
- ext-json: *
- google/cloud-pubsub: ^1.24
- illuminate/broadcasting: ^7.0
- illuminate/config: ^7.0
- illuminate/contracts: ^7.0
- illuminate/log: ^7.0
- illuminate/support: ^7.0
- jag/laravel-contracts-google-pubsub: ^0.1.3
- jag/laravel-exceptions-google-pubsub: ^0.1.2
Requires (Dev)
- orchestra/testbench: ^5.2
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-11-06 11:57:22 UTC
README
Laravel Broadcaster using Google PubSub
⚠️ NOTE: Currently on development, changes may drastically occur without further notice
Requirements
- PHP
^7.1
- Laravel/Lumen
^7.0
(support older version?: issue#1) - gRPC (Optional but increase performance)
Getting Started
Install Composer
composer require jag/laravel-broadcaster-google-pubsub
Add Service Provider
Since Laravel 5.5 Auto Discovery is enabled by default, but in case you disable or uses Lumen Framework, add the service provider:
On config/app.php
... 'providers' => [ ... Jag\Broadcaster\GooglePubSub\Providers\LaravelPubSubServiceProvider::class, ] ...
If Lumen however, on your bootstrap/app.php
... $app->register(Jag\Broadcaster\GooglePubSub\Providers\LaravelPubSubServiceProvider::class); ...
Configuration
In Laravel & Lumen
Make sure your BROADCAST_DRIVER
is google
On your .env
...
BROADCAST_DRIVER=google
GOOGLE_PUBSUB_BROADCASTER_PROJECT_ID=insert-your-google-project-id-here
GOOGLE_PUBSUB_BROADCASTER_CREDENTIALS=path/to/your/key.json
In Lumen
In case you are using Lumen, you need to copy broadcasting configuration usually found at vendor/laravel/lumen-framework/config/broadcasting.php
to your config/broadcasting.php
, then add these configuration:
return [ 'default' => env('BROADCAST_DRIVER', 'null'), 'connections' => [ // Usually other connections here like pusher, redis, log & null by default 'google' => [ 'driver' => 'google', 'project_id' => env('GOOGLE_PUBSUB_BROADCASTER_PROJECT_ID', env('GOOGLE_PROJECT_ID', env('GCLOUD_PROJECT'))), 'credentials_path' => env('GOOGLE_PUBSUB_BROADCASTER_CREDENTIALS', env('GOOGLE_APPLICATION_CREDENTIALS')), 'auto_create_topic' => env('GOOGLE_PUBSUB_BROADCASTER_AUTO_CREATE_TOPIC', false), 'override_config' => [], ] ], ];
You can also find these configuration at vendor/jag/laravel-broadcaster-google-pubsub/config/google.php
Configuration
GOOGLE_PUBSUB_BROADCASTER_CREDENTIALS
ifnull
or not present, will automatically search forstorage/key.json
.
If you also add like this
GOOGLE_PUBSUB_BROADCASTER_CREDENTIALS=storage/google-key.json
in your.env
, it will search forstorage/google-key.json
, just a magic use ofStr::startsWith
documentation.
You can enable auto creation of topic in
auto_create_topic
(boolean type, default false
), BUT make sure your credentials have permission ofPub/Sub Viewer
.
You can also override most of all PubSubClient related configuration on
override_config
EXCEPTprojectId
:
// broadcasting.php return [ ... 'connections' => [ 'google' => [ ...// Default configuration 'override_config' => [ 'retries' => 5, 'requestTimeout' => 120, ], ] ], ];
Usage
To use these in your Events, make sure to implement Illuminate\Contracts\Broadcasting\ShouldBroadcast
and add the topic on broadcastOn()
.
// App\Events\NewlyCreatedProductEvent.php ... class NewlyCreatedProductEvent implements ShouldBroadcast { ... public function broadcastOn() { return [ 'text-based-topic-name', new ProductChannel() ]; } }
On your channel, the topic name will be based on channel's name
// App\Broadcasting\ProductChannel ... class ProductChanel extends Channel { public function __construct() { parent::__construct('product-topic'); } }
But you can also override this by $topic
public property.
// App\Broadcasting\ProductChannel ... class ProductChanel extends Channel { // or the fully name topic eg. projects/project-id/topics/topic-id public $topic = 'override-topic-name'; public function __construct() { parent::__construct('product-topic'); } }