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.
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.
require"google/cloud/pubsub"pubsub=Google::Cloud::PubSub.new(project_id:"my-project",credentials:"/path/to/keyfile.json")# 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
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 supported
by Ruby Core—that is, Ruby versions that are either in normal maintenance or in
security maintenance, and not end of life. Older versions of Ruby may still
work, but are unsupported and not recommended. See
https://www.ruby-lang.org/en/downloads/branches/ for details about the Ruby
support schedule.
Please note that this project is released with a Contributor Code of Conduct. By
participating in this project you agree to abide by its terms. See Code of
Conduct
for more information.
License
This library is licensed under Apache 2.0. Full license text is available in
LICENSE.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["Version latestkeyboard_arrow_down\n\n- [3.0.2 (latest)](/ruby/docs/reference/google-cloud-pubsub/latest)\n- [3.0.1](/ruby/docs/reference/google-cloud-pubsub/3.0.1)\n- [2.23.0](/ruby/docs/reference/google-cloud-pubsub/2.23.0)\n- [2.22.0](/ruby/docs/reference/google-cloud-pubsub/2.22.0)\n- [2.21.0](/ruby/docs/reference/google-cloud-pubsub/2.21.0)\n- [2.20.0](/ruby/docs/reference/google-cloud-pubsub/2.20.0)\n- [2.19.0](/ruby/docs/reference/google-cloud-pubsub/2.19.0)\n- [2.18.1](/ruby/docs/reference/google-cloud-pubsub/2.18.1)\n- [2.17.0](/ruby/docs/reference/google-cloud-pubsub/2.17.0)\n- [2.16.0](/ruby/docs/reference/google-cloud-pubsub/2.16.0)\n- [2.15.5](/ruby/docs/reference/google-cloud-pubsub/2.15.5)\n- [2.14.0](/ruby/docs/reference/google-cloud-pubsub/2.14.0)\n- [2.12.1](/ruby/docs/reference/google-cloud-pubsub/2.12.1)\n- [2.11.0](/ruby/docs/reference/google-cloud-pubsub/2.11.0)\n- [2.10.0](/ruby/docs/reference/google-cloud-pubsub/2.10.0)\n- [2.9.2](/ruby/docs/reference/google-cloud-pubsub/2.9.2) \n\ngoogle-cloud-pubsub\n===================\n\n[Google Cloud Pub/Sub](https://cloud.google.com/pubsub/) ([docs](https://cloud.google.com/pubsub/docs/reference/rest/)) 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.\n\n- Full set of examples and detailed docs in the [google-cloud-pubsub API documentation](https://googleapis.dev/ruby/google-cloud-pubsub/latest)\n- [google-cloud-pubsub on RubyGems](https://rubygems.org/gems/google-cloud-pubsub)\n- [General Google Cloud Pub/Sub documentation](https://cloud.google.com/pubsub/docs)\n\nQuick Start\n-----------\n\n $ gem install google-cloud-pubsub\n\nAuthentication\n--------------\n\nThis 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.\n\nInstructions and configuration options are covered in the [Authentication Guide](https://googleapis.dev/ruby/google-cloud-pubsub/latest/file.AUTHENTICATION.html).\n\nExample\n-------\n\n```ruby\nrequire \"google/cloud/pubsub\"\n\npubsub = Google::Cloud::PubSub.new(\n project_id: \"my-project\",\n credentials: \"/path/to/keyfile.json\"\n)\n\n# Get a publisher for a topic\npublisher = pubsub.publisher \"my-topic\"\n\n# Publish a new message\nmsg = publisher.publish \"new-message\"\n\n# Get a subscriber for a subscription\nsubscriber = pubsub.subscriber \"my-topic-sub\"\n\n# Create a listener to listen for available messages\n# By default, this block will be called on 8 concurrent threads.\n# This can be changed with the :threads option\nlistener = subscriber.listen do |received_message|\n # process message\n puts \"Data: #{received_message.message.data}, published at #{received_message.message.published_at}\"\n received_message.acknowledge!\nend\n\n# Handle exceptions from listener\nlistener.on_error do |exception|\n puts \"Exception: #{exception.class} #{exception.message}\"\nend\n\n# Gracefully shut down the subscriber on program exit, blocking until\n# all received messages have been processed or 10 seconds have passed\nat_exit do\n listener.stop!(10)\nend\n\n# Start background threads that will call the block passed to listen.\nlistener.start\n\n# Block, letting processing threads continue in the background\nsleep\n```\n\nEnabling Logging\n----------------\n\nTo enable logging for this library, set the logger for the underlying [gRPC](https://github.com/grpc/grpc/tree/master/src/ruby) library. The logger that you set may be a Ruby stdlib [`Logger`](https://ruby-doc.org/current/stdlibs/logger/Logger.html) as shown below, or a [`Google::Cloud::Logging::Logger`](https://googleapis.dev/ruby/google-cloud-logging/latest) that will write logs to [Stackdriver Logging](https://cloud.google.com/logging/). See [grpc/logconfig.rb](https://github.com/grpc/grpc/blob/master/src/ruby/lib/grpc/logconfig.rb) and the gRPC [spec_helper.rb](https://github.com/grpc/grpc/blob/master/src/ruby/spec/spec_helper.rb) for additional information.\n\nConfiguring a Ruby stdlib logger: \n\n```ruby\nrequire \"logger\"\n\nmodule MyLogger\n LOGGER = Logger.new $stderr, level: Logger::WARN\n def logger\n LOGGER\n end\nend\n\n# Define a gRPC module-level logger method before grpc/logconfig.rb loads.\nmodule GRPC\n extend MyLogger\nend\n```\n\nSupported Ruby Versions\n-----------------------\n\nThis library is supported on Ruby 3.1+.\n\nGoogle provides official support for Ruby versions that are actively supported\nby Ruby Core---that is, Ruby versions that are either in normal maintenance or in\nsecurity maintenance, and not end of life. Older versions of Ruby *may* still\nwork, but are unsupported and not recommended. See\n\u003chttps://www.ruby-lang.org/en/downloads/branches/\u003e for details about the Ruby\nsupport schedule.\n\nVersioning\n----------\n\nThis library follows [Semantic Versioning](http://semver.org/).\n\nThis library is considered to be stable and will not have backwards-incompatible changes introduced in subsequent minor releases.\n\nContributing\n------------\n\nContributions to this library are always welcome and highly encouraged.\n\nSee the [Contributing\nGuide](https://googleapis.dev/ruby/google-cloud-pubsub/latest/file.CONTRIBUTING.html)\nfor more information on how to get started.\n\nPlease note that this project is released with a Contributor Code of Conduct. By\nparticipating in this project you agree to abide by its terms. See [Code of\nConduct](https://googleapis.dev/ruby/google-cloud-pubsub/latest/file.CODE_OF_CONDUCT.html)\nfor more information.\n\nLicense\n-------\n\nThis library is licensed under Apache 2.0. Full license text is available in\n[LICENSE](https://googleapis.dev/ruby/google-cloud-pubsub/latest/file.LICENSE.html).\n\nSupport\n-------\n\nPlease [report bugs at the project on\nGithub](https://github.com/googleapis/google-cloud-ruby/issues). Don't\nhesitate to [ask\nquestions](http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby)\nabout the client or APIs on [StackOverflow](http://stackoverflow.com)."]]