Module: Google::Cloud::PubSub

Defined in:
lib/google/cloud/pubsub.rb,
lib/google/cloud/pubsub/errors.rb,
lib/google/cloud/pubsub/convert.rb,
lib/google/cloud/pubsub/message.rb,
lib/google/cloud/pubsub/project.rb,
lib/google/cloud/pubsub/service.rb,
lib/google/cloud/pubsub/version.rb,
lib/google/cloud/pubsub/publisher.rb,
lib/google/cloud/pubsub/subscriber.rb,
lib/google/cloud/pubsub/credentials.rb,
lib/google/cloud/pubsub/admin_clients.rb,
lib/google/cloud/pubsub/publish_result.rb,
lib/google/cloud/pubsub/async_publisher.rb,
lib/google/cloud/pubsub/batch_publisher.rb,
lib/google/cloud/pubsub/flow_controller.rb,
lib/google/cloud/pubsub/internal_logger.rb,
lib/google/cloud/pubsub/message_listener.rb,
lib/google/cloud/pubsub/received_message.rb,
lib/google/cloud/pubsub/acknowledge_result.rb,
lib/google/cloud/pubsub/async_publisher/batch.rb,
lib/google/cloud/pubsub/message_listener/stream.rb,
lib/google/cloud/pubsub/message_listener/inventory.rb,
lib/google/cloud/pubsub/message_listener/sequencer.rb,
lib/google/cloud/pubsub/message_listener/enumerator_queue.rb,
lib/google/cloud/pubsub/message_listener/timed_unary_buffer.rb

Overview

Google Cloud Pub/Sub

Google Cloud Pub/Sub 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.

See Google Cloud Pub/Sub Overview.

Defined Under Namespace

Modules: SubscriptionAdmin, TopicAdmin Classes: AcknowledgeResult, AsyncPublisher, AsyncPublisherStopped, BatchPublisher, Credentials, FlowControlLimitError, Message, MessageListener, OrderedMessageDeliveryError, OrderedMessagesDisabled, OrderingKeyError, Project, PublishResult, Publisher, ReceivedMessage, Subscriber

Constant Summary collapse

DEFAULT_COMPRESS =
false
DEFAULT_COMPRESSION_BYTES_THRESHOLD =
240
VERSION =
"3.3.0".freeze

Class Method Summary collapse

Class Method Details

.configure {|Google::Cloud.configure.pubsub| ... } ⇒ Google::Cloud::Config

Configure the Google Cloud PubSub library.

The following PubSub configuration parameters are supported:

  • project_id - (String) Identifier for a PubSub project.
  • credentials - (String, Hash, Google::Auth::Credentials) The path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object. (See Credentials)
  • scope - (String, Array) The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access.
  • quota_project - (String) The project ID for a project that can be used by client libraries for quota and billing purposes.
  • timeout - (Numeric) Default timeout to use in requests.
  • endpoint - (String) Override of the endpoint host name, or nil to use the default endpoint.
  • emulator_host - (String) Host name of the emulator. Defaults to ENV["PUBSUB_EMULATOR_HOST"]
  • on_error - (Proc) A Proc to be run when an error is encountered on a background thread. The Proc must take the error object as the single argument. (See Google::Cloud::PubSub::MessageListener#on_error.)

Yields:

Returns:

  • (Google::Cloud::Config)

    The configuration object the Google::Cloud::PubSub library uses.



159
160
161
162
163
# File 'lib/google/cloud/pubsub.rb', line 159

def self.configure
  yield Google::Cloud.configure.pubsub if block_given?

  Google::Cloud.configure.pubsub
end

.new(project_id: nil, credentials: nil, scope: nil, timeout: nil, universe_domain: nil, endpoint: nil, emulator_host: nil, logger: nil) ⇒ Google::Cloud::PubSub::Project

Creates a new object for connecting to the Pub/Sub service. Each call creates a new connection.

For more information on connecting to Google Cloud see the Authentication Guide.

Examples:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

publisher = pubsub.publisher "my-topic"
publisher.publish "task completed"

Parameters:

  • project_id (String) (defaults to: nil)

    Project identifier for the Pub/Sub service you are connecting to. If not present, the default project for the credentials is used.

  • credentials (Google::Auth::Credentials) (defaults to: nil)

    A Google::Auth::Credentials object. (See Credentials) @note Warning: Passing a String to a keyfile path or a Hash of credentials is deprecated. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data.

    @example

    # The recommended way to provide credentials is to use the make_creds method # on the appropriate credentials class for your environment.

    require "googleauth"

    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

  • scope (String, Array<String>) (defaults to: nil)

    The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access. See Using OAuth 2.0 to Access Google APIs.

    The default scope is:

    • https://www.googleapis.com/auth/pubsub
  • timeout (Numeric) (defaults to: nil)

    Default timeout to use in requests. Optional.

  • universe_domain (String) (defaults to: nil)

    A custom universe domain. Optional.

  • endpoint (String) (defaults to: nil)

    Override of the endpoint host name. Optional. If the param is nil, uses the default endpoint.

  • emulator_host (String) (defaults to: nil)

    Pub/Sub emulator host. Optional. If the param is nil, uses the value of the emulator_host config.

  • logger (Logger) (defaults to: nil)

    Optional Logger instance for emitting library-level debug logs. If not provided, it will default to configure.logger, which defaults to Logger.new STDOUT if not set. To enable logging, set environment variable GOOGLE_SDK_RUBY_LOGGING_GEMS to "all" or a comma separated list of gem names, including "pubsub".

Returns:

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/google/cloud/pubsub.rb', line 95

def self.new project_id: nil,
             credentials: nil,
             scope: nil,
             timeout: nil,
             universe_domain: nil,
             endpoint: nil,
             emulator_host: nil,
             logger: nil
  project_id ||= default_project_id
  scope ||= configure.scope
  timeout ||= configure.timeout
  endpoint ||= configure.endpoint
  universe_domain ||= configure.universe_domain
  emulator_host ||= configure.emulator_host
  logger ||= configure.logger

  if emulator_host
    credentials = :this_channel_is_insecure
    endpoint = emulator_host
  else
    credentials ||= default_credentials scope: scope
    unless credentials.is_a? Google::Auth::Credentials
      credentials = PubSub::Credentials.new credentials, scope: scope
    end
  end

  project_id ||= credentials.project_id if credentials.respond_to? :project_id
  project_id = project_id.to_s # Always cast to a string
  raise ArgumentError, "project_id is missing" if project_id.empty?

  logger = Google::Cloud::PubSub::InternalLogger.new logger
  service = PubSub::Service.new project_id, credentials,
                                host: endpoint,
                                timeout: timeout,
                                universe_domain: universe_domain,
                                logger: logger
  PubSub::Project.new service
end