# Cloud Pub/Sub API - Class Google::Cloud::PubSub::AsyncPublisher (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::AsyncPublisher.

Used to publish multiple messages in batches to a topic. See
[Publisher#async_publisher](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Publisher#Google__Cloud__PubSub__Publisher_async_publisher_instance_ "Google::Cloud::PubSub::Publisher#async_publisher (method)")

## Inherits

- Object

## Includes

- MonitorMixin

## Example

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

pubsub = Google::Cloud::PubSub.new

publisher = pubsub.publisher "my-topic"
publisher.publish_async "task completed" do |result|
  if result.succeeded?
    log_publish_success result.data
  else
    log_publish_failure result.data, result.error
  end
end

publisher.async_publisher.stop!
```

## Methods

### #callback_threads

    def callback_threads() -> Numeric

The number of threads to handle the published messages' callbacks.
Default is 4.
**Returns**

- (Numeric) --- the current value of callback_threads

### #enable_message_ordering!

    def enable_message_ordering!()

Enables message ordering for messages with ordering keys. When
enabled, messages published with the same `ordering_key` will be
delivered in the order they were published.


<br />

See [#message_ordering?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_message_ordering__instance_ "Google::Cloud::PubSub::AsyncPublisher#message_ordering? (method)"). See [Publisher#publish_async](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Publisher#Google__Cloud__PubSub__Publisher_publish_async_instance_ "Google::Cloud::PubSub::Publisher#publish_async (method)"),
[Subscriber#listen](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Subscriber#Google__Cloud__PubSub__Subscriber_listen_instance_ "Google::Cloud::PubSub::Subscriber#listen (method)"), and [Message#ordering_key](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Message#Google__Cloud__PubSub__Message_ordering_key_instance_ "Google::Cloud::PubSub::Message#ordering_key (method)").

### #flow_control

    def flow_control()

Returns the value of attribute flow_control.

### #flush

    def flush() -> AsyncPublisher

Forces all messages in the current batch to be published
immediately.
**Returns**

- ([AsyncPublisher](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher)) --- returns self so calls can be chained.

### #interval

    def interval() -> Numeric

The number of seconds to collect messages before the batch is published. Default
is 0.01.
**Returns**

- (Numeric) --- the current value of interval

### #max_bytes

    def max_bytes() -> Integer

The maximum size of messages to be collected before the batch is published.
Default is 1,000,000 (1MB).
**Returns**

- (Integer) --- the current value of max_bytes

### #max_messages

    def max_messages() -> Integer

The maximum number of messages to be collected before the batch is
published. Default is 100.
**Returns**

- (Integer) --- the current value of max_messages

### #message_ordering?

    def message_ordering?() -> Boolean

Whether message ordering for messages with ordering keys has been
enabled. When enabled, messages published with the same `ordering_key`
will be delivered in the order they were published. When disabled,
messages may be delivered in any order.


<br />

See [#enable_message_ordering!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_enable_message_ordering__instance_ "Google::Cloud::PubSub::AsyncPublisher#enable_message_ordering! (method)"). See [Publisher#publish_async](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Publisher#Google__Cloud__PubSub__Publisher_publish_async_instance_ "Google::Cloud::PubSub::Publisher#publish_async (method)"),
[Subscriber#listen](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Subscriber#Google__Cloud__PubSub__Subscriber_listen_instance_ "Google::Cloud::PubSub::Subscriber#listen (method)"), and [Message#ordering_key](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Message#Google__Cloud__PubSub__Message_ordering_key_instance_ "Google::Cloud::PubSub::Message#ordering_key (method)").
**Returns**

- (Boolean)

### #publish

    def publish(data = nil, attributes = nil, ordering_key: nil, **extra_attrs, &callback) { |result| ... }

Add a message to the async publisher to be published to the topic.
Messages will be collected in batches and published together.
See [Publisher#publish_async](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Publisher#Google__Cloud__PubSub__Publisher_publish_async_instance_ "Google::Cloud::PubSub::Publisher#publish_async (method)")
**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.
**Yields**

- (result) --- the callback for when the message has been published
**Yield Parameter**

- **result** ([PublishResult](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-PublishResult)) --- the result of the asynchronous publish
**Raises**

- ([Google::Cloud::PubSub::AsyncPublisherStopped](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisherStopped)) --- when the publisher is stopped. (See [#stop](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_stop_instance_ "Google::Cloud::PubSub::AsyncPublisher#stop (method)") and [#stopped?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_stopped__instance_ "Google::Cloud::PubSub::AsyncPublisher#stopped? (method)").)
- ([Google::Cloud::PubSub::OrderedMessagesDisabled](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-OrderedMessagesDisabled)) --- when publishing a message with an `ordering_key` but ordered messages are not enabled. (See [#message_ordering?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_message_ordering__instance_ "Google::Cloud::PubSub::AsyncPublisher#message_ordering? (method)") and [#enable_message_ordering!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_enable_message_ordering__instance_ "Google::Cloud::PubSub::AsyncPublisher#enable_message_ordering! (method)").)
- ([Google::Cloud::PubSub::OrderingKeyError](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-OrderingKeyError)) --- when publishing a message with an `ordering_key` that has already failed when publishing. Use [#resume_publish](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_resume_publish_instance_ "Google::Cloud::PubSub::AsyncPublisher#resume_publish (method)") to allow this `ordering_key` to be published again.

### #publish_threads

    def publish_threads() -> Numeric

The number of threads used to publish messages. Default is 2.
**Returns**

- (Numeric) --- the current value of publish_threads

### #resume_publish

    def resume_publish(ordering_key) -> boolean

Resume publishing ordered messages for the provided ordering key.
**Parameter**

- **ordering_key** (String) --- Identifies related messages for which publish order should be respected.
**Returns**

- (boolean) --- `true` when resumed, `false` otherwise.

### #started?

    def started?() -> boolean

Whether the publisher has been started.
**Returns**

- (boolean) --- `true` when started, `false` otherwise.

### #stop

    def stop() -> AsyncPublisher

Begins the process of stopping the publisher. Messages already in
the queue will be published, but no new messages can be added. Use
[#wait!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_wait__instance_ "Google::Cloud::PubSub::AsyncPublisher#wait! (method)") to block until the publisher is fully stopped and all
pending messages have been published.
**Returns**

- ([AsyncPublisher](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher)) --- returns self so calls can be chained.

### #stop!

    def stop!(timeout = nil) -> AsyncPublisher

Stop this publisher and block until the publisher is fully stopped,
all pending messages have been published, and all callbacks have
completed, or until `timeout` seconds have passed.


<br />

The same as calling [#stop](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_stop_instance_ "Google::Cloud::PubSub::AsyncPublisher#stop (method)") and [#wait!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_wait__instance_ "Google::Cloud::PubSub::AsyncPublisher#wait! (method)").
**Parameter**

- **timeout** (Number, nil) --- The number of seconds to block until the publisher is fully stopped. Default will block indefinitely.
**Returns**

- ([AsyncPublisher](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher)) --- returns self so calls can be chained.

### #stopped?

    def stopped?() -> boolean

Whether the publisher has been stopped.
**Returns**

- (boolean) --- `true` when stopped, `false` otherwise.

### #topic_name

    def topic_name() -> String

The name of the topic the messages are published to. The value is a
fully-qualified topic name in the form `projects/{project_id}/topics/{topic_id}`.
**Returns**

- (String) --- the current value of topic_name

### #wait!

    def wait!(timeout = nil) -> AsyncPublisher

Blocks until the publisher is fully stopped, all pending messages have
been published, and all callbacks have completed, or until `timeout`
seconds have passed.


<br />

Does not stop the publisher. To stop the publisher, first call [#stop](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_stop_instance_ "Google::Cloud::PubSub::AsyncPublisher#stop (method)")
and then call [#wait!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher#Google__Cloud__PubSub__AsyncPublisher_wait__instance_ "Google::Cloud::PubSub::AsyncPublisher#wait! (method)") to block until the publisher is stopped
**Parameter**

- **timeout** (Number, nil) --- The number of seconds to block until the publisher is fully stopped. Default will block indefinitely.
**Returns**

- ([AsyncPublisher](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AsyncPublisher)) --- returns self so calls can be chained.

## Constants

### PUBLISH_RETRY_ERRORS

**value:** \[
GRPC::Cancelled, GRPC::DeadlineExceeded, GRPC::Internal,
GRPC::ResourceExhausted, GRPC::Unauthenticated, GRPC::Unavailable
\].freeze  
rubocop:enable Metrics/AbcSize