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.

Quick Start

$ gem install google-cloud-pubsub


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 the Authentication Guide.


require "google/cloud/pubsub"

pubsub =
  project_id: "my-project",
  credentials: "/path/to/keyfile.json"

# Retrieve a topic
topic = pubsub.topic "my-topic"

# Publish a new message
msg = topic.publish "new-message"

# Retrieve a subscription
sub = pubsub.subscription "my-topic-sub"

# Create a subscriber to listen for available messages
# By default, this block will be called on 8 concurrent threads.
# This can be changed with the :threads option
subscriber = sub.listen do |received_message|
  # process message
  puts "Data: #{}, published at #{received_message.message.published_at}"

# Handle exceptions from listener
subscriber.on_error do |exception|
  puts "Exception: #{exception.class} #{exception.message}"

# Gracefully shut down the subscriber on program exit, blocking until
# all received messages have been processed or 10 seconds have passed
at_exit do

# Start background threads that will call the block passed to listen.

# Block, letting processing threads continue in the background

Enabling Logging

To enable logging for this library, set the logger for the underlying gRPC library. The logger that you set may be a Ruby stdlib Logger as shown below, or a Google::Cloud::Logging::Logger that will write logs to Stackdriver Logging. See grpc/logconfig.rb and the gRPC spec_helper.rb for additional information.

Configuring a Ruby stdlib logger:

require "logger"

module MyLogger
  LOGGER = $stderr, level: Logger::WARN
  def logger

# Define a gRPC module-level logger method before grpc/logconfig.rb loads.
module GRPC
  extend MyLogger

Supported Ruby Versions

This library is supported on Ruby 2.5+.

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. Currently, this means Ruby 2.5 and later. Older versions of Ruby may still work, but are unsupported and not recommended. See for details about the Ruby support schedule.


This library follows Semantic Versioning.

It is currently in major version zero (0.y.z), which means that anything may change at any time and the public API should not be considered stable.


Contributions to this library are always welcome and highly encouraged.

See the Contributing Guide for more information on how to get started.

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.


This library is licensed under Apache 2.0. Full license text is available in LICENSE.


Please report bugs at the project on Github. Don't hesitate to ask questions about the client or APIs on StackOverflow.