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

MessageListener object used to stream and process messages from a
Subscriber. See [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)")

## Inherits

- Object

## Includes

- MonitorMixin

## Example

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"

listener = subscriber.listen do |received_message|
  # process message
  received_message.acknowledge!
end

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

# Shut down the subscriber when ready to stop receiving messages.
listener.stop!
```

## Methods

### #callback

    def callback() -> Proc

The procedure that will handle the messages
received from the subscription.
**Returns**

- (Proc) --- the current value of callback

### #callback_threads

    def callback_threads() -> Integer

The number of threads used to
handle the received messages. Default is 8.
**Returns**

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

### #deadline

    def deadline() -> Numeric

The default number of seconds the stream
will hold received messages before modifying the message's ack
deadline. The minimum is 10, the maximum is 600. Default is 60.
**Returns**

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

### #last_error

    def last_error() -> Exception, nil

The most recent unhandled error to occur while listening to messages
on the listener.


<br />

If an unhandled error has occurred the listener will attempt to
recover from the error and resume listening.
**Returns**

- (Exception, nil) --- error The most recent error raised.
**Example**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"

listener = subscriber.listen do |received_message|
  # process message
  received_message.acknowledge!
end

# Start listening for messages and errors.
listener.start

# If an error was raised, it can be retrieved here:
listener.last_error #=> nil

# Shut down the subscriber when ready to stop receiving messages.
listener.stop!
```

### #max_duration_per_lease_extension

    def max_duration_per_lease_extension() -> Integer

The maximum amount of time in seconds for a single lease extension attempt. Bounds the delay before a message
redelivery if the listener fails to extend the deadline. Default is 0 (disabled).
**Returns**

- (Integer) --- The maximum number of seconds.

### #max_outstanding_bytes

    def max_outstanding_bytes() -> Integer

The total byte size of received messages to be collected by listener. Default is 100,000,000 (100MB).
**Returns**

- (Integer) --- The maximum number of bytes.

### #max_outstanding_messages

    def max_outstanding_messages() -> Integer

The number of received messages to be collected by listener. Default is 1,000.
**Returns**

- (Integer) --- The maximum number of messages.

### #max_total_lease_duration

    def max_total_lease_duration() -> Integer

The number of seconds that received messages can be held awaiting processing. Default is 3,600 (1 hour).
**Returns**

- (Integer) --- The maximum number of seconds.

### #message_ordering

    def message_ordering() -> Boolean

Whether message ordering has
been enabled.
**Returns**

- (Boolean) --- the current value of message_ordering

### #min_duration_per_lease_extension

    def min_duration_per_lease_extension() -> Integer

The minimum amount of time in seconds for a single lease extension attempt. Bounds the delay before a message
redelivery if the listener fails to extend the deadline. Default is 0 (disabled).
**Returns**

- (Integer) --- The minimum number of seconds.

### #on_error

    def on_error(&block) { |error| ... }

Register to be notified of errors when raised.


If an unhandled error has occurred the listener will attempt to
recover from the error and resume listening.

<br />

Multiple error handlers can be added.
**Yields**

- (callback) --- The block to be called when an error is raised.
**Yield Parameter**

- **error** (Exception) --- The error raised.
**Example**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"

listener = subscriber.listen do |received_message|
  # process message
  received_message.acknowledge!
end

# Register to be notified when unhandled errors occur.
listener.on_error do |error|
  # log error
  puts error
end

# Start listening for messages and errors.
listener.start

# Shut down the subscriber when ready to stop receiving messages.
listener.stop!
```

### #push_threads

    def push_threads() -> Integer

The number of threads to handle
acknowledgement ([ReceivedMessage#ack!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_ack__instance_ "Google::Cloud::PubSub::ReceivedMessage#ack! (method)")) and delay messages
([ReceivedMessage#nack!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_nack__instance_ "Google::Cloud::PubSub::ReceivedMessage#nack! (method)"), [ReceivedMessage#modify_ack_deadline!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_modify_ack_deadline__instance_ "Google::Cloud::PubSub::ReceivedMessage#modify_ack_deadline! (method)")).
Default is 4.
**Returns**

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

### #start

    def start() -> MessageListener

Starts the listener pulling from the subscription and processing the
received messages.
**Returns**

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

### #started?

    def started?() -> boolean

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

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

### #stop

    def stop() -> MessageListener

Immediately stops the listener. No new messages will be pulled from
the subscription. Use [#wait!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-MessageListener#Google__Cloud__PubSub__MessageListener_wait__instance_ "Google::Cloud::PubSub::MessageListener#wait! (method)") to block until all received messages have
been processed or released: All actions taken on received messages that
have not yet been sent to the API will be sent to the API. All received
but unprocessed messages will be released back to the API and redelivered.
**Returns**

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

### #stop!

    def stop!(timeout = nil) -> MessageListener

Stop this listener and block until the listener is fully stopped
and all received messages have been processed or released, 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-MessageListener#Google__Cloud__PubSub__MessageListener_stop_instance_ "Google::Cloud::PubSub::MessageListener#stop (method)") and [#wait!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-MessageListener#Google__Cloud__PubSub__MessageListener_wait__instance_ "Google::Cloud::PubSub::MessageListener#wait! (method)").
**Parameter**

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

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

### #stopped?

    def stopped?() -> boolean

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

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

### #streams

    def streams() -> Integer

The number of concurrent streams to open
to pull messages from the subscription. Default is 1.
**Returns**

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

### #subscription_name

    def subscription_name() -> String

The name of the subscription the
messages are pulled from.
**Returns**

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

### #wait!

    def wait!(timeout = nil) -> MessageListener

Blocks until the listener is fully stopped and all received messages
have been processed or released, or until `timeout` seconds have
passed.


<br />

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

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

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