# Cloud Pub/Sub API - Class Google::Cloud::PubSub::BatchPublisher (v3.3.0)

3.3.0 (latest) 3.2.1 3.1.1 3.0.3 2.23.0 2.22.0 2.21.0 2.20.0 2.19.0 2.18.1 2.17.0 2.16.0 2.15.5 2.14.0 2.12.1 2.11.0 2.10.0 2.9.2 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

```ruby
require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

publisher = pubsub.publisher "my-topic"
msgs = publisher.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:

```ruby
require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

publisher = pubsub.publisher "my-topic"
msgs = publisher.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
```