# Cloud PubSub Client - Class Message (2.19.3)

2.19.3 (latest) 2.19.2 2.18.0 2.17.0 2.16.2 2.15.1 2.14.0 2.13.2 2.12.0 2.11.3 2.10.1 2.9.1 2.8.2 2.7.0 2.6.0 2.5.2 2.4.0 2.3.0 2.2.1 2.1.2 1.50.0 1.49.0 1.48.0 1.47.0 1.46.5 1.45.2 1.44.0 1.43.2 1.42.1 1.41.3 1.40.1 1.39.3 Reference documentation and code samples for the Cloud PubSub Client class Message.

Represents a PubSub Message.

Example:

    use Google\Cloud\PubSub\PubSubClient;

    $pubsub = new PubSubClient();
    $subscription = $pubsub->subscription('my-new-subscription');

    $messages = $subscription->pull();
    foreach ($messages as $message) {
        echo $message->data();
    }

## Namespace

Google \\ Cloud \\ PubSub

## Methods

### __construct

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `message` | `array` Message Options See [PubsubMessage](https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage). |
| `↳ data` | `string` The message data field. If this is empty, the message must contain at least one attribute. |
| `↳ attributes` | `array` Optional attributes for this message. |
| `↳ messageId` | `string` ID of this message, assigned by the server when the message is published. |
| `↳ publishTime` | `string` The time at which the message was published, populated by the server when it receives the publish call. |
| `↳ orderingKey` | `string` The message ordering key. |
| `metadata` | `array` Message metadata |
| `↳ ackId` | `string` The message ackId. This is only set when messages are pulled from the PubSub service. |
| `↳ deliveryAttempt` | `int` Delivery attempt counter is 1 + (the sum of number of NACKs and number of ack_deadline exceeds) for this message. If a DeadLetterPolicy is not set on the subscription, this will be `null`. |
| `↳ subscription` | `https://docs.cloud.google.com/php/docs/reference/cloud-pubsub/latest/Subscription` The subscription the message was obtained from. This is only set when messages are delivered by pushDelivery |

### data

The message payload.

Example:

    echo $message->data();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### attribute

Retrieve a single message attribute.

Example:

    echo $message->attribute('browser-name');

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `key` | `string` The attribute key |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string|null` |   |

### attributes

Retrieve all message attributes.

Example:

    $attributes = $message->attributes();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array` |   |

### id

Get the message ID.

The message ID is assigned by the server when the message is published.
Guaranteed to be unique within the topic.

Example:

    echo $message->id();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### orderingKey

Get the message ordering key.

Example:

    $orderingKey = $message->orderingKey();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string|null` |   |

### publishTime

Get the message published time.

Example:

    $time = $message->publishTime();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `DateTimeInterface|null` |   |

### ackId

Get the message ackId.

This is only set when message is obtained via
[Subscription::pull()](https://docs.cloud.google.com/php/docs/reference/cloud-pubsub/latest/Subscription#_Google_Cloud_PubSub_Subscription__pull__).

Example:

    echo $message->ackId();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `string` |   |

### deliveryAttempt

Get the delivery attempt count.

If a DeadLetterPolicy is not set on the subscription, this will be `null`.

Example:

    echo $message->deliveryAttempt();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `int|null` |   |

### subscription

Get the subcription through which the message was obtained.

This is only set when the message is obtained via push delivery.

Example:

    echo "Subscription Name: ". $message->subscription()->name();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-pubsub/latest/Subscription` |   |

### info

Get the message data.

Available keys are `ackId`, `subscription` and `message`.

Example:

    $info = $message->info();

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array` |   |

### toArray

Get the message as an array.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array` |   |