|
| 1 | +# php-pubsub-google-cloud |
| 2 | + |
| 3 | +A Google Cloud adapter for the [php-pubsub](https://github.com/Superbalist/php-pubsub) package. |
| 4 | + |
| 5 | +[](https://twitter.com/superbalist) |
| 6 | +[](https://travis-ci.org/Superbalist/php-pubsub-google-cloud) |
| 7 | +[](LICENSE) |
| 8 | +[](https://packagist.org/packages/superbalist/php-pubsub-google-cloud) |
| 9 | +[](https://packagist.org/packages/superbalist/php-pubsub-google-cloud) |
| 10 | + |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +composer require superbalist/php-pubsub-google-cloud |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```php |
| 21 | +putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/../your-gcloud-key.json'); |
| 22 | + |
| 23 | +$client = new \Google\Cloud\PubSub\PubSubClient([ |
| 24 | + 'projectId' => 'your-project-id-here', |
| 25 | +]); |
| 26 | + |
| 27 | +$adapter = new \Superbalist\PubSub\GoogleCloud\GoogleCloudPubSubAdapter($client); |
| 28 | + |
| 29 | +// consume messages |
| 30 | +// note: this is a blocking call |
| 31 | +$adapter->subscribe('my_channel', function ($message) { |
| 32 | + var_dump($message); |
| 33 | +}); |
| 34 | + |
| 35 | +// publish messages |
| 36 | +$adapter->publish('my_channel', 'HELLO WORLD'); |
| 37 | +$adapter->publish('my_channel', json_encode(['hello' => 'world'])); |
| 38 | +$adapter->publish('my_channel', 1); |
| 39 | +$adapter->publish('my_channel', false); |
| 40 | +``` |
| 41 | + |
| 42 | +## Examples |
| 43 | + |
| 44 | +The library comes with [examples](examples) for the adapter and a [Dockerfile](Dockerfile) for |
| 45 | +running the example scripts. |
| 46 | + |
| 47 | +Run `make up`. |
| 48 | + |
| 49 | +You will start at a `bash` prompt in the `/opt/php-pubsub` directory. |
| 50 | + |
| 51 | +If you need another shell to publish a message to a blocking consumer, you can run `docker-compose run php-pubsub-google-cloud /bin/bash` |
| 52 | + |
| 53 | +To run the examples: |
| 54 | +```bash |
| 55 | +$ php examples/GoogleCloudConsumerExample.php |
| 56 | +$ php examples/GoogleCloudPublishExample.php (in a separate shell) |
| 57 | +``` |
0 commit comments