Google Pubsub emulator with python

2.7k Views Asked by At

Does anyone have a very basic pub/sub example for Python that uses the emulator.

This here is my subscriber code

## setup subscribers
from google.cloud import pubsub

print("subscribing to topic")


subscriber = pubsub.SubscriberClient()
subscription_path = subscriber.subscription_path(app.config['PUB_SUB_PROJECT'], app.config['PUB_SUB_TOPIC'])

def callback(message):
    print('Received message: {}'.format(message))


subscriber.subscribe(subscription_path, callback=callback)

And then here is my code for publishing

from google.cloud import pubsub
publisher = pubsub.PublisherClient()
topic_path = publisher.topic_path(app.config['PUB_SUB_PROJECT'], app.config['PUB_SUB_TOPIC'])
try:
    topic = publisher.create_topic(topic_path)
except Exception:
    app.logger.info("Topic already exists")
data = "ein test"
data = data.encode('utf-8')
publisher.publish(topic_path, data=data)
print("published topic")

It seems that publishing works -> but I think it's actually publishing to the cloud queue and not to the emulator. Therefor my subscriber never receives anything.

Any tipps and tricks are welcome. I believe it's as simple as ensuring that the publisher publishes to the emulator and the subscriber reads from the emulator.

1

There are 1 best solutions below

0
On

In Python you don't need to make any code changes to use the emulator. Instead, you must have the PUBSUB_EMULATOR_HOST and PUBSUB_PROJECT_ID environmental variables defined.

The easiest way to set them is to run $(gcloud beta emulators pubsub env-init) before starting your program. if you are using Google App Engine locally, run that command and then start your app with dev_appserver.py app.yaml --env_var PUBSUB_EMULATOR_HOST=${PUBSUB_EMULATOR_HOST}.

This is documented at https://cloud.google.com/pubsub/docs/emulator