Skip to content

Commit 059bfcd

Browse files
committed
Merge pull request GoogleCloudPlatform#16 from bshaffer/add-pubsub
PubSub example for PHP
2 parents a0c403f + 59514bb commit 059bfcd

File tree

14 files changed

+2037
-2
lines changed

14 files changed

+2037
-2
lines changed

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ before_install:
2727
- php dump_credentials.php
2828

2929
script:
30+
# run bigquery tests
3031
- cd bigquery/api
3132
- composer install
3233
- phpunit mainTest.php
@@ -36,4 +37,9 @@ script:
3637
- cd datastore
3738
- composer install
3839
- phpunit test
39-
- cd ..
40+
- cd ..
41+
# run pubsub tests
42+
- cd pubsub
43+
- composer install
44+
- phpunit test
45+
- cd ..

datastore/src/DatastoreHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DatastoreHelper
2828

2929
/**
3030
* Creates a query object for pulling the last $limit number of
31-
* "PubSubMessage" items, equivalent to the following GQL:
31+
* "DatastoreComment" items, equivalent to the following GQL:
3232
*
3333
* SELECT * from DatastoreComment ORDER BY created DESC LIMIT 20
3434
*

pubsub/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Google PubSub PHP Sample Application
2+
3+
## Description
4+
5+
Note: The push endpoints don't work with the App Engine's local
6+
devserver. The push notifications will go to an HTTP URL on the App
7+
Engine server even when you run this sample locally. So we recommend
8+
you deploy and run the app on App Engine.
9+
TODO(tmatsuo): Better implementation for devserver.
10+
11+
## Register your application
12+
13+
- Go to
14+
[Google Developers Console](https://console.developers.google.com/project)
15+
and create a new project. This will automatically enable an App
16+
Engine application with the same ID as the project.
17+
18+
- Enable the "Google Cloud Pub/Sub" API under "APIs & auth > APIs."
19+
- Enable the "Google Cloud Datastore" API under "APIs & auth > APIs."
20+
- For local development also follow the instructions below.
21+
- Go to "Credentials" and create a new Service Account.
22+
- Select "Generate new JSON key", then download a new JSON file.
23+
- Set the following environment variable:
24+
- `GOOGLE_APPLICATION_CREDENTIALS`: the file path to the downloaded JSON file.
25+
26+
## Prerequisites
27+
28+
- Install [`composer`](https://getcomposer.org)
29+
- Install the App Engine Python SDK.
30+
We recommend you install
31+
[Cloud SDK](https://developers.google.com/cloud/sdk/) rather than
32+
just installing App Engine SDK.
33+
34+
- Install Google API client library for PHP into 'lib' directory by running:
35+
36+
```
37+
$ composer install
38+
```
39+
40+
## Configuration
41+
42+
Run the following commands to create your pubsub subscription topic:
43+
44+
```sh
45+
$ gcloud alpha pubsub topics create [your-topic-name]
46+
1 topic(s) created successfully
47+
- projects/[your-project-name]/topics/[your-topic-name]
48+
```
49+
50+
> We use **`php-pubsub-example`** as the topic name, but you can change this
51+
> by setting the `PUBSUB_TOPIC` environment variable (see `app.yaml`)
52+
53+
Then you need to create your subscription to this topic by supplying
54+
the endpoint to be notified when the topic is published to:
55+
56+
```
57+
gcloud alpha pubsub subscriptions create [your-subscription-name] \
58+
--topic [your-topic-name] \
59+
--push-endpoint https://[your-project-name].appspot.com/receive_message \
60+
–-ack-deadline 30
61+
```
62+
63+
## Deploy the application to App Engine
64+
65+
```
66+
$ gcloud preview app deploy app.yaml --set-default --project YOUR_PROJECT_NAME
67+
```
68+
69+
Then access the following URL:
70+
https://{YOUR_PROJECT_NAME}.appspot.com/
71+
72+
## Run the application locally
73+
74+
```
75+
$ dev_appserver.py -A your-project-name .
76+
```
77+
78+
## Contributing changes
79+
80+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
81+
82+
## Licensing
83+
84+
* See [LICENSE](../LICENSE)
85+
86+

pubsub/app.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
runtime: php55
2+
api_version: 1
3+
threadsafe: true
4+
5+
handlers:
6+
- url: /js
7+
static_dir: web/js
8+
- url: /.*
9+
script: web/index.php
10+
11+
env_variables:
12+
GOOGLE_PROJECT_NAME: "YOUR_PROJECT_NAME"

pubsub/composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"autoload": {
3+
"psr-4": {
4+
"Google\\Cloud\\Samples\\Pubsub\\": "src"
5+
}
6+
},
7+
"require": {
8+
"php": ">=5.4",
9+
"silex/silex": "~1.3",
10+
"twig/twig": "~1.8|~2.0",
11+
"symfony/twig-bridge": "~2.7|3.0.*",
12+
"google/apiclient": "~2.0@rc"
13+
},
14+
"require-dev": {
15+
"symfony/dom-crawler": "~2.0",
16+
"symfony/css-selector": "~2.0",
17+
"symfony/browser-kit": "~2.7"
18+
}
19+
}

0 commit comments

Comments
 (0)