# Cloud PubSub Client - Class BatchPublisher (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

> [!WARNING]
> **Beta**
>
>
> This library is covered by the [Pre-GA Offerings Terms](https://docs.cloud.google.com/terms/service-terms#1)
> of the Terms of Service. Pre-GA libraries might have limited support,
> and changes to pre-GA libraries might not be compatible with other pre-GA versions.
> For more information, see the
> [launch stage descriptions](https://docs.cloud.google.com/products#product-launch-stages).

Reference documentation and code samples for the Cloud PubSub Client class BatchPublisher.

Publishes messages to Google Cloud Pub\\Sub with background batching.

Example:

    use Google\Cloud\PubSub\PubSubClient;

    $pubsub = new PubSubClient();
    $batchPublisher = $pubsub->topic('my_topic')
        ->batchPublisher();

    $batchPublisher->publish([
        'data' => 'An important message.'
    ]);

## Namespace

Google \\ Cloud \\ PubSub

## Methods

### __construct

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `topicName` | `string` The topic name. |
| `options` | `array` Please see [Topic::batchPublisher()](https://docs.cloud.google.com/php/docs/reference/cloud-pubsub/latest/Topic#_Google_Cloud_PubSub_Topic__batchPublisher__) for configuration details. |
| `↳ enableCompression` | `bool` Flag to enable compression of messages before publishing. Set the flag to `true` to enable compression. Defaults to `false`. Messsages are compressed if their total size \>= `compressionBytesThreshold`, whose default value has been experimentally derived after performance evaluations. |
| `↳ compressionBytesThreshold` | `int` The threshold byte size above which messages are compressed. This only takes effect if `enableCompression` is set to `true`. Defaults to `240`. |

### publish

Send messages to a batch queue.

Example:

    $batchPublisher->publish([
        'data' => 'An important message.'
    ]);

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `message` | `https://docs.cloud.google.com/php/docs/reference/cloud-pubsub/latest/Message|array` An instance of [Message](https://docs.cloud.google.com/php/docs/reference/cloud-pubsub/latest/Message), or an array in the correct [Message Format](https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage). |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `void` |   |

### publishDeferred

See also:

- [Publish Message](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/publish)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `messages` | `array[]` A list of messages. Each message must be in the correct [Message Format](https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage). |
| `options` | `array` \[optional\] Configuration Options |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array` | A list of message IDs. |

## Constants

### ID_TEMPLATE

    Value: 'pubsub-topic-%s'