Cloud Pub/Sub API - Class Google::Cloud::PubSub::BatchPublisher (v2.16.0)

Reference documentation and code samples for the Cloud Pub/Sub API class Google::Cloud::PubSub::BatchPublisher.

Topic Batch Publisher object used to publish multiple messages at once.

Inherits

  • Object

Example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

topic = pubsub.topic "my-topic"
msgs = topic.publish do |batch_publisher|
  batch_publisher.publish "task 1 completed", foo: :bar
  batch_publisher.publish "task 2 completed", foo: :baz
  batch_publisher.publish "task 3 completed", foo: :bif
end

Methods

#publish

def publish(data, attributes = nil, ordering_key: nil, **extra_attrs)

Add a message to the batch to be published to the topic. All messages added to the batch will be published at once. See Topic#publish

Parameters
  • data (String, File) — The message payload. This will be converted to bytes encoded as ASCII-8BIT.
  • attributes (Hash) — Optional attributes for the message.
  • ordering_key (String) (defaults to: nil) — Identifies related messages for which publish order should be respected.
Example

Multiple messages can be sent at the same time using a block:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

topic = pubsub.topic "my-topic"
msgs = topic.publish do |batch_publisher|
  batch_publisher.publish "task 1 completed", foo: :bar
  batch_publisher.publish "task 2 completed", foo: :baz
  batch_publisher.publish "task 3 completed", foo: :bif
end