Google Cloud Pub/Sub Emulator

To develop and test your application locally, you can use the Google Cloud Pub/Sub emulator, which provides local emulation of the production Google Cloud Pub/Sub environment. You can start the Google Cloud Pub/Sub emulator using the gcloud command-line tool. However, you need to do a small amount of configuration before running your application against the emulator, such as starting the emulator and setting environment variables.

Prerequisites

You must have the following to use the Google Cloud Pub/Sub emulator:

  • A Java JRE (version 7 or greater) installed and available in your system PATH.

  • Google Cloud SDK installed. Cloud SDK contains the gcloud command-line tool.

  • An application that is built using the Google Cloud Client Libraries.

Starting the Google Cloud Pub/Sub emulator

You start the emulator by invoking pubsub start from a command prompt:

gcloud beta emulators pubsub start [options]

where [options] are command line arguments supplied to the gcloud command-line tool.

One of the options you can supply is --data-dir=<directory>, where <directory> is the directory you want to use.

After you start the emulator, you can confirm it is running by seeing a message similar to:

...
[pubsub] This is the Google Pub/Sub fake.
[pubsub] Implementation may be incomplete or differ from the real system.
...
[pubsub] INFO: Server started, listening on 8538

Setting environment variables

Run the env-init command to determine and set the environment variables used by the emulator. Note that after following these instructions, the Google Cloud Pub/Sub client libraries in your code call the API running in the local instance instead of the production API.

Linux

To automatically set the variables, run the env-init command using command substitution:

$ (gcloud beta emulators pubsub env-init)

To manually set the variables, run the env-init command:

$ gcloud beta emulators pubsub env-init

and then create the environment variables and values by executing the commands in the output of the env-init command. For example (values may be different based on your environment):

$ export PUBSUB_EMULATOR_HOST=localhost:8590

Note that these variables may change in future releases or in between invocations of the emulator.

Windows

Run the env-init command:

gcloud beta emulators pubsub env-init

and then create the environment variables by executing the commands in the output of the env-init command. For example (values may be different based on your environment):

set PUBSUB_EMULATOR_HOST=localhost:8432

Stopping the Google Cloud Pub/Sub emulator

To stop the emulator, press Ctrl+C.

Google Cloud Pub/Sub emulator command-line arguments

For details on command-line arguments for the Google Cloud Pub/Sub emulator, see gcloud beta emulators pubsub.

Send feedback about...

Cloud Pub/Sub Documentation