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

## ReceivedMessage

Represents a Pub/Sub [Message](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Message "Google::Cloud::PubSub::Message (class)") that can be acknowledged or delayed.

## Inherits

- Object

## Example

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data
  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

### #ack!

    def ack!() { |Result| ... }

**Alias Of** : [#acknowledge!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_acknowledge!_instance_) Acknowledges receipt of the message.
**Yields**

- (callback) --- The block to be called when reject operation is done.
**Yield Parameter**

- **Result** ([Google::Cloud::PubSub::AcknowledgeResult](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AcknowledgeResult)) --- object that contains the status and error.
**Examples**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  received_message.acknowledge! do |result|
    puts result.status
  end
end

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

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

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  received_message.acknowledge!
end

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

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

### #ack_id

    def ack_id()

The acknowledgment ID for the message.

### #acknowledge!

    def acknowledge!() { |Result| ... }

**Aliases**

- [#ack!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_ack!_instance_)
Acknowledges receipt of the message.
**Yields**

- (callback) --- The block to be called when reject operation is done.
**Yield Parameter**

- **Result** ([Google::Cloud::PubSub::AcknowledgeResult](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AcknowledgeResult)) --- object that contains the status and error.
**Examples**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  received_message.acknowledge! do |result|
    puts result.status
  end
end

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

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

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  received_message.acknowledge!
end

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

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

### #attributes

    def attributes()

Optional attributes for the received message.

### #data

    def data()

The received message payload. This data is a list of bytes encoded as
ASCII-8BIT.

### #delivery_attempt

    def delivery_attempt() -> Integer, nil

Returns the delivery attempt counter for the message. If a dead letter policy is not set on the subscription,
this will be `nil`.


The delivery attempt counter is `1 + (the sum of number of NACKs and number of ack_deadline exceeds)` for the
message.

A NACK is any call to `ModifyAckDeadline` with a `0` deadline. An `ack_deadline` exceeds event is whenever a
message is not acknowledged within `ack_deadline`. Note that `ack_deadline` is initially
`Subscription.ackDeadlineSeconds`, but may get extended automatically by the client library.

<br />

The first delivery of a given message will have this value as `1`. The value is calculated at best effort and
is approximate.
**Returns**

- (Integer, nil) --- A delivery attempt value of `1` or greater, or `nil` if a dead letter policy is not set on the subscription.
**Example**

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

pubsub = Google::Cloud::PubSub.new

subscription_admin = pubsub.subscription_admin

subscription = subscription_admin.create_subscription \
  name: pubsub.subscription_path("my-topic-sub"),
  topic: pubsub.topic_path("my-topic"),
  dead_letter_policy: {
    dead_letter_topic: pubsub.topic_path("my-dead-letter-topic"),
    max_delivery_attempts: 10
  }

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

listener = subscriber.listen do |received_message|
  puts received_message.message.delivery_attempt
end
```

### #ignore!

    def ignore!() { |Result| ... }

**Alias Of** : [#reject!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_reject!_instance_) Resets the acknowledge deadline for the message without acknowledging
it.


<br />

This will make the message available for redelivery.
**Yields**

- (callback) --- The block to be called when reject operation is done.
**Yield Parameter**

- **Result** ([Google::Cloud::PubSub::AcknowledgeResult](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AcknowledgeResult)) --- object that contains the status and error.
**Examples**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Release message back to the API.
  received_message.reject! do |result|
    puts result.status
  end
end

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

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

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Release message back to the API.
  received_message.reject!
end

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

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

### #message

    def message()

**Aliases**

- [#msg](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_msg_instance_)
The received message.

### #message_id

    def message_id()

**Aliases**

- [#msg_id](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_msg_id_instance_)
The ID of the received message, assigned by the server at publication
time. Guaranteed to be unique within the topic.

### #modify_ack_deadline!

    def modify_ack_deadline!(new_deadline) { |Result| ... }

Modifies the acknowledge deadline for the message.


<br />

This indicates that more time is needed to process the message, or to
make the message available for redelivery.
**Parameter**

- **new_deadline** (Integer) --- The new ack deadline in seconds from the time this request is sent to the Pub/Sub system. Must be \>= 0. For example, if the value is `10`, the new ack deadline will expire 10 seconds after the call is made. Specifying `0` may immediately make the message available for another pull request.
**Yields**

- (callback) --- The block to be called when reject operation is done.
**Yield Parameter**

- **Result** ([Google::Cloud::PubSub::AcknowledgeResult](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AcknowledgeResult)) --- object that contains the status and error.
**Examples**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Delay for 2 minutes
  received_message.modify_ack_deadline! 120 do |result|
    puts result.status
  end
end

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

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

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Delay for 2 minutes
  received_message.modify_ack_deadline! 120
end

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

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

### #msg

    def msg()

**Alias Of** : [#message](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_message_instance_) The received message.

### #msg_id

    def msg_id()

**Alias Of** : [#message_id](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_message_id_instance_) The ID of the received message, assigned by the server at publication
time. Guaranteed to be unique within the topic.

### #nack!

    def nack!() { |Result| ... }

**Alias Of** : [#reject!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_reject!_instance_) Resets the acknowledge deadline for the message without acknowledging
it.


<br />

This will make the message available for redelivery.
**Yields**

- (callback) --- The block to be called when reject operation is done.
**Yield Parameter**

- **Result** ([Google::Cloud::PubSub::AcknowledgeResult](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AcknowledgeResult)) --- object that contains the status and error.
**Examples**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Release message back to the API.
  received_message.reject! do |result|
    puts result.status
  end
end

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

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

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Release message back to the API.
  received_message.reject!
end

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

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

### #ordering_key

    def ordering_key() -> String

Identifies related messages for which publish order should be
respected.


Google Cloud Pub/Sub ordering keys provide the ability to ensure
related messages are sent to subscribers in the order in which they
were published. Messages can be tagged with an ordering key, a string
that identifies related messages for which publish order should be
respected. The service guarantees that, for a given ordering key and
publisher, messages are sent to subscribers in the order in which they
were published. Ordering does not require sacrificing high throughput
or scalability, as the service automatically distributes messages for
different ordering keys across subscribers.

<br />

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)") and [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)").
**Returns**

- (String)

### #publish_time

    def publish_time()

**Alias Of** : [#published_at](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_published_at_instance_) The time at which the message was published.

### #published_at

    def published_at()

**Aliases**

- [#publish_time](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_publish_time_instance_)
The time at which the message was published.

### #reject!

    def reject!() { |Result| ... }

**Aliases**

- [#nack!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_nack!_instance_)
- [#ignore!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage#Google__Cloud__PubSub__ReceivedMessage_ignore!_instance_)
Resets the acknowledge deadline for the message without acknowledging
it.


<br />

This will make the message available for redelivery.
**Yields**

- (callback) --- The block to be called when reject operation is done.
**Yield Parameter**

- **Result** ([Google::Cloud::PubSub::AcknowledgeResult](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-AcknowledgeResult)) --- object that contains the status and error.
**Examples**

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Release message back to the API.
  received_message.reject! do |result|
    puts result.status
  end
end

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

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

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

pubsub = Google::Cloud::PubSub.new

subscriber = pubsub.subscriber "my-topic-sub"
listener = subscriber.listen do |received_message|
  puts received_message.message.data

  # Release message back to the API.
  received_message.reject!
end

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

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