google-cloud-pubsub
Google Cloud Pub/Sub (docs) is designed to provide reliable, many-to-many, asynchronous messaging between applications. 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.
- Full set of examples and detailed docs in thegoogle-cloud-pubsub API documentation
- google-cloud-pubsub on RubyGems
- General Google Cloud Pub/Sub documentation
Quick Start
$ gem install google-cloud-pubsubAuthentication
This library uses Service Account credentials to connect to Google Cloud services. When running on Google Cloud Platform (GCP), including Google Compute Engine (GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google Cloud Functions (GCF) and Cloud Run, the credentials will be discovered automatically. When running on other environments the Service Account credentials can be specified by providing the path to the JSON file, or the JSON itself, in environment variables.
Instructions and configuration options are covered in theAuthentication Guide.
Example
require"googleauth"require"google/cloud/pubsub"credentials=::Google::Auth::ServiceAccountCredentials.make_creds(json_key_io:::File.open("/path/to/keyfile.json"))pubsub=Google::Cloud::PubSub.new(project_id:"my-project",credentials:credentials)# Get a publisher for a topicpublisher=pubsub.publisher"my-topic"# Publish a new messagemsg=publisher.publish"new-message"# Get a subscriber for a subscriptionsubscriber=pubsub.subscriber"my-topic-sub"# Create a listener to listen for available messages# By default, this block will be called on 8 concurrent threads.# This can be changed with the :threads optionlistener=subscriber.listendo|received_message|# process messageputs"Data:#{received_message.message.data}, published at#{received_message.message.published_at}"received_message.acknowledge!end# Handle exceptions from listenerlistener.on_errordo|exception|puts"Exception:#{exception.class}#{exception.message}"end# Gracefully shut down the subscriber on program exit, blocking until# all received messages have been processed or 10 seconds have passedat_exitdolistener.stop!(10)end# Start background threads that will call the block passed to listen.listener.start# Block, letting processing threads continue in the backgroundsleep
Enabling Logging
To enable logging for this library, set the logger for the underlyinggRPC library. The logger that you set may be a Ruby stdlibLogger as shown below, or aGoogle::Cloud::Logging::Logger that will write logs toStackdriver Logging. Seegrpc/logconfig.rb and the gRPCspec_helper.rb for additional information.
Configuring a Ruby stdlib logger:
require"logger"moduleMyLoggerLOGGER=Logger.new$stderr,level:Logger::WARNdefloggerLOGGERendend# Define a gRPC module-level logger method before grpc/logconfig.rb loads.moduleGRPCextendMyLoggerend
Supported Ruby Versions
This library is supported on Ruby 3.1+.
Google provides official support for Ruby versions that are actively supportedby Ruby Core—that is, Ruby versions that are either in normal maintenance or insecurity maintenance, and not end of life. Older versions of Rubymay stillwork, but are unsupported and not recommended. Seehttps://www.ruby-lang.org/en/downloads/branches/ for details about the Rubysupport schedule.
Versioning
This library followsSemantic Versioning.
This library is considered to be stable and will not have backwards-incompatible changes introduced in subsequent minor releases.
Contributing
Contributions to this library are always welcome and highly encouraged.
See theContributingGuidefor more information on how to get started.
Please note that this project is released with a Contributor Code of Conduct. Byparticipating in this project you agree to abide by its terms. SeeCode ofConductfor more information.
License
This library is licensed under Apache 2.0. Full license text is available inLICENSE.
Support
Pleasereport bugs at the project onGithub. Don'thesitate toaskquestionsabout the client or APIs onStackOverflow.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-01-30 UTC.