Search code examples
pythonhttpgetclientgoogle-cloud-pubsub

How to pull GCP Pub/Sub messages with a python HTTP server?


GCP provides a REST API for Pub/Sub: https://cloud.google.com/pubsub/docs/reference/rest

The Python API allows users to pull messages as follows, using the Python library https://googleapis.dev/python/pubsub/latest/index.html

import os
from google.cloud import pubsub_v1

topic_name = 'projects/{project_id}/topics/{topic}'.format(
    project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
    topic='MY_TOPIC_NAME',  # Set this to something appropriate.
)

subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
    project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
    sub='MY_SUBSCRIPTION_NAME',  # Set this to something appropriate.
)

def callback(message):
    print(message.data)
    message.ack()

with pubsub_v1.SubscriberClient() as subscriber:
    subscriber.create_subscription(
        name=subscription_name, topic=topic_name)
    future = subscriber.subscribe(subscription_name, callback)
    try:
        future.result()
    except KeyboardInterrupt:
        future.cancel()

Do users have to use this library however?

I would like to use a different HTTP client to parse these messages via GET requests using the Pub/Sub API. In principle, this should be done with something like the requests library, using the get method:

import requests

x = requests.get(END_POINT)
print(x.status_code)

However, will this work with streaming GET requests from Google Pub/Sub?


Solution

  • It's not a GET, but a post, and yes you can use API to pull messages. You have the documentation here

    Don't forget to ack the messages or to modify the AckDeadline

    All is API at Google Cloud, but the libraries can help your to perform things easily. but if you prefer reinventing the wheel, why not!