{"id":2051,"library":"google-cloud-pubsublite","title":"Google Cloud Pub/Sub Lite Client Library","description":"Google Cloud Pub/Sub Lite is a specialized messaging service for high-volume, low-cost event ingestion and delivery with partitioned topics. This Python client library, currently at version 1.13.0, provides a programmatic interface to interact with Pub/Sub Lite topics, subscriptions, and administrative operations, receiving regular updates from Google.","status":"active","version":"1.13.0","language":"en","source_language":"en","source_url":"https://github.com/googleapis/python-pubsublite","tags":["google-cloud","pubsub","pubsub-lite","messaging","serverless"],"install":[{"cmd":"pip install google-cloud-pubsublite","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Client classes are nested under `cloudpubsub` or `admin` modules.","wrong":"from google.cloud.pubsublite import PublisherClient","symbol":"PublisherClient","correct":"from google.cloud.pubsublite.cloudpubsub import PublisherClient"},{"note":"Client classes are nested under `cloudpubsub` or `admin` modules.","wrong":"from google.cloud.pubsublite import SubscriberClient","symbol":"SubscriberClient","correct":"from google.cloud.pubsublite.cloudpubsub import SubscriberClient"},{"note":"Client classes are nested under `cloudpubsub` or `admin` modules.","wrong":"from google.cloud.pubsublite import AdminClient","symbol":"AdminClient","correct":"from google.cloud.pubsublite.admin import AdminClient"},{"note":"Path and configuration types are under the `types` module.","wrong":"from google.cloud.pubsublite import TopicPath","symbol":"TopicPath","correct":"from google.cloud.pubsublite.types import TopicPath"},{"note":"Flow control settings are part of common types, not specific client modules.","wrong":"from google.cloud.pubsublite.cloudpubsub import FlowControlSettings","symbol":"FlowControlSettings","correct":"from google.cloud.pubsublite.types import FlowControlSettings"}],"quickstart":{"code":"import os\nimport time\nfrom concurrent.futures import TimeoutError\n\nfrom google.cloud.pubsublite.cloudpubsub import PublisherClient, SubscriberClient\nfrom google.cloud.pubsublite.types import (\n    CloudZone,\n    TopicPath,\n    SubscriptionPath,\n    FlowControlSettings,\n)\n\n# Configuration: Ensure GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_ZONE are set\n# or replace placeholders with your actual GCP Project ID and a Lite zone (e.g., 'us-central1-a').\nproject_id = os.environ.get(\"GOOGLE_CLOUD_PROJECT\", \"your-gcp-project-id\")\nzone = os.environ.get(\"GOOGLE_CLOUD_ZONE\", \"us-central1-a\")\ntopic_id = \"my-pubsublite-topic\"\nsubscription_id = \"my-pubsublite-subscription\"\n\n# Define topic and subscription paths\ntopic_path = TopicPath(project_id, CloudZone(zone), topic_id)\nsubscription_path = SubscriptionPath(project_id, CloudZone(zone), subscription_id)\n\nif project_id == \"your-gcp-project-id\":\n    print(\"Please set the GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_ZONE environment variables.\")\n    print(\"Alternatively, replace 'your-gcp-project-id' and 'us-central1-a' with your actual values.\")\n    exit(1)\n\nprint(f\"Using Topic: {topic_path}\")\nprint(f\"Using Subscription: {subscription_path}\")\nprint(\"Ensure the topic and subscription already exist in your Pub/Sub Lite project.\")\n\n# --- Publisher: Send a message ---\nprint(\"\\n--- Publishing messages ---\")\npublisher = PublisherClient()\nmessage_data = b\"Hello from Pub/Sub Lite Python client!\"\ntry:\n    # publish returns a future; .result() waits for acknowledgement\n    future = publisher.publish(topic_path, message_data)\n    message_id = future.result(timeout=5) \n    print(f\"Published message '{message_data.decode()}' with ID: {message_id}\")\nexcept TimeoutError:\n    print(\"Publishing timed out.\")\nexcept Exception as e:\n    print(f\"Error publishing message: {e}\")\nfinally:\n    publisher.close() # Important to close the client to flush messages\n    print(\"Publisher client closed.\")\n\n# Allow a moment for the message to propagate\ntime.sleep(2)\n\n# --- Subscriber: Receive messages ---\nprint(\"\\n--- Subscribing to messages ---\")\nsubscriber = SubscriberClient()\n# Configure flow control to limit outstanding messages and bytes\nflow_control_settings = FlowControlSettings(\n    messages_outstanding=10, bytes_outstanding=10 * 1024 * 1024  # 10 MiB\n)\n\nreceived_messages = []\n\ndef callback(message):\n    payload = message.data.decode()\n    print(f\"Received message: '{payload}' (ID: {message.message_id})\")\n    received_messages.append(payload)\n    message.ack() # Acknowledge the message to allow it to be discarded\n\n# The subscribe method returns a streaming_future that can be cancelled.\nstreaming_future = subscriber.subscribe(subscription_path, callback, flow_control_settings)\nprint(f\"Listening for messages on {subscription_path} for 10 seconds...\")\n\ntry:\n    # Wait for the future to complete (e.g., if cancelled) or timeout.\n    streaming_future.result(timeout=10) \nexcept TimeoutError:\n    print(\"No more messages received within 10 seconds or subscription completed.\")\nexcept Exception as e:\n    print(f\"Error during subscription: {e}\")\nfinally:\n    streaming_future.cancel() # Stop the subscription stream\n    subscriber.close() # Important to close the client to release resources\n    print(\"Subscriber client closed.\")\n\nif received_messages:\n    print(f\"\\nSuccessfully processed {len(received_messages)} message(s).\")\nelse:\n    print(\"\\nNo messages were processed by the subscriber.\")","lang":"python","description":"This quickstart demonstrates how to publish a message to a Pub/Sub Lite topic and then subscribe to and consume that message from a subscription. It assumes you have already created the necessary Pub/Sub Lite topic and subscription within your GCP project and set the required environment variables (GOOGLE_CLOUD_PROJECT, GOOGLE_CLOUD_ZONE)."},"warnings":[{"fix":"Upgrade to v1.8.2+ and explicitly set `publish_idempotence=True` in `PublisherSettings` or pass `id` to `publish` if desired.","message":"Publish idempotence, introduced in v1.8.0, was disabled by default in v1.8.2. If you relied on automatic idempotence, you must now explicitly enable it via `PublisherClient.publish(..., id='your-id')` or `PublisherSettings(..., publish_idempotence=True)`.","severity":"gotcha","affected_versions":"<=1.8.2"},{"fix":"Upgrade to `google-cloud-pubsublite` v1.13.0 or later to ensure stable dependency versions are used.","message":"Older versions of `google-cloud-pubsublite` (prior to v1.13.0) might inadvertently install pre-release versions of dependencies, leading to potential instability or unexpected behavior.","severity":"gotcha","affected_versions":"<1.13.0"},{"fix":"Always call `client.close()` when you are done with a client instance. For asynchronous operations, consider using `async with` for context management.","message":"Client objects (`PublisherClient`, `SubscriberClient`, `AdminClient`) should always be explicitly closed using their `.close()` method to ensure all resources are released and pending operations (like publishing messages) are completed.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your environment is running Python 3.8 or a later compatible version.","message":"The `google-cloud-pubsublite` library requires Python 3.8 or newer. Using older Python versions will result in installation failures or runtime errors.","severity":"gotcha","affected_versions":"<3.8 Python runtime"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}