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.