google-cloud-pubsub 2.31.1


pip install google-cloud-pubsub

  Latest version

Released: Jul 28, 2025

Project Links

Meta
Author: Google LLC
Requires Python: >=3.7

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Programming Language
  • Python
  • Python :: 3
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13

Operating System
  • OS Independent

Topic
  • Internet

GA pypi versions

Google Cloud Pub / Sub is a fully-managed real-time messaging service that allows you to send and receive messages between independent applications. You can leverage Cloud Pub/Sub’s flexibility to decouple systems and components hosted on Google Cloud Platform or elsewhere on the Internet. By building on the same technology Google uses, Cloud Pub / Sub is designed to provide “at least once” delivery at low latency with on-demand scalability to 1 million messages per second (and beyond).

Publisher applications can send messages to a topic and other applications can subscribe to that topic to receive the messages. By decoupling senders and receivers, Google Cloud Pub/Sub allows developers to communicate between independently written applications.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.

  2. Enable billing for your project.

  3. Enable the Google Cloud Pub / Sub API.

  4. Setup Authentication.

Installation

Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.

With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.

Supported Python Versions

Python >= 3.7

Deprecated Python Versions

Python <= 3.6.

The last version of this library compatible with Python 2.7 is google-cloud-pubsub==1.7.0.

Mac/Linux

pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-pubsub

Windows

pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-pubsub

Example Usage

Publishing

To publish data to Cloud Pub/Sub you must create a topic, and then publish messages to it

import os
from google.cloud import pubsub_v1

publisher = pubsub_v1.PublisherClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
    project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
    topic='MY_TOPIC_NAME',  # Set this to something appropriate.
)
publisher.create_topic(name=topic_name)
future = publisher.publish(topic_name, b'My first message!', spam='eggs')
future.result()

To learn more, consult the publishing documentation.

Subscribing

To subscribe to data in Cloud Pub/Sub, you create a subscription based on the topic, and subscribe to that, passing a callback function.

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)

The future returned by the call to subscriber.subscribe can be used to block the current thread until a given condition obtains:

try:
    future.result()
except KeyboardInterrupt:
    future.cancel()

It is also possible to pull messages in a synchronous (blocking) fashion. To learn more about subscribing, consult the subscriber documentation.

Authentication

It is possible to specify the authentication method to use with the Pub/Sub clients. This can be done by providing an explicit Credentials instance. Support for various authentication methods is available from the google-auth library.

For example, to use JSON Web Tokens, provide a google.auth.jwt.Credentials instance:

import json
from google.auth import jwt

service_account_info = json.load(open("service-account-info.json"))
audience = "https://pubsub.googleapis.com/google.pubsub.v1.Subscriber"

credentials = jwt.Credentials.from_service_account_info(
    service_account_info, audience=audience
)

subscriber = pubsub_v1.SubscriberClient(credentials=credentials)

# The same for the publisher, except that the "audience" claim needs to be adjusted
publisher_audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher"
credentials_pub = credentials.with_claims(audience=publisher_audience)
publisher = pubsub_v1.PublisherClient(credentials=credentials_pub)

Versioning

This library follows Semantic Versioning.

It is currently in major version one (1.y.z), which means that the public API should be considered stable.

Contributing

Contributions to this library are always welcome and highly encouraged.

See the CONTRIBUTING doc for more information on how to get started.

Community

The best place to ask questions is via Stackoverflow: https://stackoverflow.com/questions/tagged/google-cloud-pubsub

License

Apache 2.0 - See the LICENSE for more information.

2.31.1 Jul 28, 2025
2.31.0 Jul 08, 2025
2.30.0 Jun 09, 2025
2.29.1 Jun 03, 2025
2.29.0 Mar 20, 2025
2.28.0 Jan 30, 2025
2.27.3 Jan 24, 2025
2.27.2 Jan 06, 2025
2.27.1 Nov 13, 2024
2.27.0 Nov 13, 2024
2.26.1 Oct 15, 2024
2.26.0 Oct 09, 2024
2.25.2 Sep 30, 2024
2.25.1 Sep 30, 2024
2.25.0 Sep 29, 2024
2.23.1 Sep 09, 2024
2.23.0 Jul 29, 2024
2.22.0 Jul 06, 2024
2.21.5 Jun 20, 2024
2.21.4 Jun 18, 2024
2.21.3 Jun 10, 2024
2.21.2 May 30, 2024
2.21.1 Apr 04, 2024
2.21.0 Mar 26, 2024
2.20.3 Mar 21, 2024
2.20.2 Mar 15, 2024
2.20.1 Mar 06, 2024
2.20.0 Mar 06, 2024
2.19.8 Mar 05, 2024
2.19.7 Feb 26, 2024
2.19.6 Feb 23, 2024
2.19.5 Feb 23, 2024
2.19.4 Feb 09, 2024
2.19.3 Feb 09, 2024
2.19.2 Feb 08, 2024
2.19.1 Feb 02, 2024
2.19.0 Dec 11, 2023
2.18.4 Sep 10, 2023
2.18.3 Aug 18, 2023
2.18.2 Aug 07, 2023
2.18.1 Jul 27, 2023
2.18.0 Jul 12, 2023
2.17.1 May 23, 2023
2.17.0 May 12, 2023
2.16.1 May 06, 2023
2.16.0 Apr 06, 2023
2.15.2 Mar 23, 2023
2.15.1 Mar 15, 2023
2.15.0 Feb 23, 2023
2.14.1 Feb 08, 2023
2.14.0 Jan 19, 2023
2.13.12 Jan 10, 2023
2.13.11 Nov 11, 2022
2.13.10 Oct 14, 2022
2.13.9 Oct 10, 2022
2.13.8 Oct 14, 2022
2.13.7 Sep 22, 2022
2.13.6 Aug 12, 2022
2.13.5 Aug 10, 2022
2.13.4 Jul 16, 2022
2.13.3 Jul 13, 2022
2.13.2 Jul 11, 2022
2.13.1 Jul 07, 2022
2.13.0 Jun 07, 2022
2.12.1 May 11, 2022
2.12.0 Apr 06, 2022
2.11.1 Jun 09, 2022
2.11.0 Mar 09, 2022
2.10.0 Mar 04, 2022
2.9.0 Nov 10, 2021
2.8.0 Sep 06, 2021
2.7.1 Aug 19, 2021
2.7.0 Jul 28, 2021
2.6.1 Jul 12, 2021
2.6.0 Jun 21, 2021
2.5.0 May 18, 2021
2.4.2 May 10, 2021
2.4.1 Mar 31, 2021
2.4.0 Feb 22, 2021
2.3.0 Feb 08, 2021
2.2.0 Dec 04, 2020
2.1.0 Sep 21, 2020
2.0.0 Sep 14, 2020
1.7.2 Jun 09, 2022
1.7.1 Apr 06, 2022
1.7.0 Jul 13, 2020
1.6.1 Jul 06, 2020
1.6.0 Jun 09, 2020
1.5.0 May 12, 2020
1.4.3 Apr 16, 2020
1.4.2 Mar 26, 2020
1.4.1 Mar 23, 2020
1.4.0 Mar 23, 2020
1.3.1 Feb 28, 2020
1.3.0 Feb 20, 2020
1.2.0 Feb 05, 2020
1.1.0 Dec 11, 2019
1.0.2 Sep 30, 2019
1.0.1 Sep 27, 2019
1.0.0 Aug 29, 2019
0.45.0 Jul 31, 2019
0.44.0 Jul 29, 2019
0.43.0 Jul 30, 2019
0.42.1 Jun 18, 2019
0.42.0 Jun 18, 2019
0.41.0 May 15, 2019
0.40.0 Mar 15, 2019
0.39.1 Dec 18, 2018
0.39.0 Nov 27, 2018
0.38.0 Sep 12, 2018
0.37.2 Aug 21, 2018
0.37.1 Aug 16, 2018
0.37.0 Aug 14, 2018
0.36.0 Aug 10, 2018
0.35.4 Jun 06, 2018
0.35.3 Jun 05, 2018
0.35.2 May 30, 2018
0.35.1 May 29, 2018
0.35.0 May 23, 2018
0.34.0 Apr 27, 2018
0.33.1 Apr 03, 2018
0.33.0 Mar 20, 2018
0.32.1 Mar 07, 2018
0.32.0 Feb 23, 2018
0.31.0 Feb 20, 2018
0.30.1 Dec 21, 2017
0.30.0 Dec 19, 2017
0.29.4 Dec 11, 2017
0.29.3 Dec 08, 2017
0.29.2 Nov 30, 2017
0.29.1 Nov 27, 2017
0.29.0 Oct 31, 2017
0.28.4 Oct 13, 2017
0.28.3 Sep 05, 2017
0.28.2 Aug 25, 2017
0.28.1 Aug 24, 2017
0.28.0 Aug 24, 2017
0.27.0 Aug 05, 2017
0.26.0 Jun 26, 2017
0.25.0 Apr 28, 2017
0.24.0 Mar 31, 2017
0.23.0 Feb 25, 2017
0.22.0 Dec 10, 2016
0.21.0 Nov 14, 2016
0.20.0 Sep 29, 2016
Extras:
Dependencies:
grpcio (<2.0.0,>=1.51.3)
google-auth (<3.0.0,>=2.14.1)
google-api-core[grpc] (!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0,>=1.34.0)
proto-plus (<2.0.0,>=1.22.0)
proto-plus (<2.0.0,>=1.22.2)
proto-plus (<2.0.0,>=1.25.0)
protobuf (!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<7.0.0,>=3.20.2)
grpc-google-iam-v1 (<1.0.0,>=0.12.4)
grpcio-status (>=1.33.2)
opentelemetry-api (<=1.22.0)
opentelemetry-api (>=1.27.0)
opentelemetry-sdk (<=1.22.0)
opentelemetry-sdk (>=1.27.0)