Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products.

Writing and Responding to Pub/Sub Messages

Region ID

The REGION_ID is an abbreviated code that Google assigns based on the region you select when you create your app. The code does not correspond to a country or province, even though some region IDs may appear similar to commonly used country and province codes. For apps created after February 2020, REGION_ID.r is included in App Engine URLs. For existing apps created before this date, the region ID is optional in the URL.

Learn more about region IDs.

Pub/Sub provides reliable, many-to-many, asynchronous messaging between applications. Publisher applications can send messages to a topic, and other applications can subscribe to that topic to receive the messages.

This document describes how to use the Cloud Client Library to send and receive Pub/Sub messages in a PHP app.


  • Follow the instructions in "Hello, World!" for PHP on App Engine to set up your environment and project, and to understand how App Engine PHP apps are structured.
  • Write down and save your project ID, because you will need it to run the sample application described in this document.

    Cloning the sample app

    Copy the sample apps to your local machine, and navigate to the pubsub directory:

    git clone
    cd php-docs-samples/pubsub

    Creating a topic and subscription

    Create a topic and subscription, which includes specifying the endpoint to which the Pub/Sub server should send requests:

    gcloud pubsub topics create YOUR_TOPIC_NAME
    gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
        --topic YOUR_TOPIC_NAME

    Setting environment variables

    Edit your index.php file to set the environment variables for your topic and subscription:

    $container->set('topic', 'php-example-topic');
    $container->set('subscription', 'php-example-subscription');

    Code review

    The sample app uses the Cloud Client Library.

    The sample app uses the values you set in the app.yaml file to configure environment variables. The push request handler uses these values to confirm that the request came from Pub/Sub and originated from a trusted source:

    runtime: php
    env: flex

    The sample app maintains a global list to store messages received by this instance:

    $messages = [];

    The pull method retrieves messages from the topic you created and adds them to the messages list:

    // get PULL pubsub messages
    $pubsub = new PubSubClient([
        'projectId' => $projectId,
    $subscription = $pubsub->subscription($subscriptionName);
    $pullMessages = [];
    foreach ($subscription->pull(['returnImmediately' => true]) as $pullMessage) {
        $pullMessages[] = $pullMessage;
        $messages[] = $pullMessage->data();
    // acknowledge PULL messages
    if ($pullMessages) {

    The publish method publishes new messages to the topic:

    if ($message = (string) $request->getBody()) {
        // Publish the pubsub message to the topic
        $pubsub = new PubSubClient([
            'projectId' => $projectId,
        $topic = $pubsub->topic($topicName);
        $topic->publish(['data' => $message]);
        return $response->withStatus(204);

    Running the sample locally

    When running locally, you can use the Google Cloud CLI to provide authentication to use Google Cloud APIs. Assuming you set up your environment as described in Prerequisites, you have already run the gcloud init command, which provides this authentication.

    Install dependencies using Composer:

    composer install

    Then set environment variables before starting your application:

    Simulating push notifications

    The application can send messages locally, but it is not able to receive push messages locally. You can, however, simulate a push message by making an HTTP request to the local push notification endpoint. The sample includes the file sample_message.json.

    You can use curl or a httpie client to send an HTTP POST request:


    After the request completes, you can refresh localhost:8080 and see the message in the list of received messages.

    Running on App Engine

    To deploy the demo app to App Engine by using the gcloud command-line tool, you run the following command from the directory where your app.yaml file is located:

    gcloud app deploy

    You can now access the application at You can use the form to submit messages, but there's no guarantee of which instance of your application will receive the notification. You can send multiple messages and refresh the page to see the received message.