Producing message batch to kafka | Laravel Kafka              

 [ K Laravel Kafka ](/) [Docs](/docs) [Blog](https://laravelkafka.com/blog) [GitHub](https://github.com/mateusjunges/laravel-kafka) 

     Search ⌘K       [    Login with GitHub Login ](https://laravelkafka.com/oauth/github/redirect) 

    Docs for version         

   selected

 v3.0     

 v2.13     

 v2.12     

 v2.11     

 v2.10     

 v2.9     

 v2.8     

 v1.13   

- - [ Introduction ](/docs/v2.8/introduction)
    - [ Requirements ](/docs/v2.8/requirements)
    - [ Installation and Setup ](/docs/v2.8/installation-and-setup)
    - [ Questions and issues ](/docs/v2.8/questions-and-issues)
    - [ Changelog ](/docs/v2.8/changelog)
    - [ Upgrade guide ](/docs/v2.8/upgrade-guide)
    - [ Example docker-compose file ](/docs/v2.8/example-docker-compose)
- Producing messages
    ------------------

    - [ Producing messages ](/docs/v2.8/producing-messages/producing-messages)
    - [ Configuring your kafka producer ](/docs/v2.8/producing-messages/configuring-producers)
    - [ Configuring message payload ](/docs/v2.8/producing-messages/configuring-message-payload)
    - [ Custom serializers ](/docs/v2.8/producing-messages/custom-serializers)
    - [ Publishing to kafka ](/docs/v2.8/producing-messages/publishing-to-kafka)
    - [ Producing message batch to kafka ](/docs/v2.8/producing-messages/producing-message-batch-to-kafka)
- Consuming messages
    ------------------

    - [ Creating a kafka consumer ](/docs/v2.8/consuming-messages/creating-consumer)
    - [ Subscribing to kafka topics ](/docs/v2.8/consuming-messages/subscribing-to-kafka-topics)
    - [ Using regex to subscribe to kafka topics ](/docs/v2.8/consuming-messages/using-regex-to-subscribe-to-kafka-topics)
    - [ Assigning consumers to a topic partition ](/docs/v2.8/consuming-messages/assigning-partitions)
    - [ Consuming messages from specific offsets ](/docs/v2.8/consuming-messages/consuming-from-specific-offsets)
    - [ Consumer groups ](/docs/v2.8/consuming-messages/consumer-groups)
    - [ Partition Discovery and Dynamic Assignment ](/docs/v2.8/consuming-messages/partition-discovery)
    - [ Message handlers ](/docs/v2.8/consuming-messages/message-handlers)
    - [ Configuring consumer options ](/docs/v2.8/consuming-messages/configuring-consumer-options)
    - [ Custom deserializers ](/docs/v2.8/consuming-messages/custom-deserializers)
    - [ Consuming messages ](/docs/v2.8/consuming-messages/consuming-messages)
    - [ Handling message batch ](/docs/v2.8/consuming-messages/handling-message-batch)
    - [ Class structure ](/docs/v2.8/consuming-messages/class-structure)
    - [ Queueable handlers ](/docs/v2.8/consuming-messages/queueable-handlers)
- Advanced usage
    --------------

    - [ Replacing the default serializer/deserializer ](/docs/v2.8/advanced-usage/replacing-default-serializer)
    - [ Graceful shutdown ](/docs/v2.8/advanced-usage/graceful-shutdown)
    - [ SASL Authentication ](/docs/v2.8/advanced-usage/sasl-authentication)
    - [ Custom Committers ](/docs/v2.8/advanced-usage/custom-committers)
    - [ Manual Commit ](/docs/v2.8/advanced-usage/manual-commit)
    - [ Middlewares ](/docs/v2.8/advanced-usage/middlewares)
    - [ Stop consumer after last messages ](/docs/v2.8/advanced-usage/stop-consumer-after-last-message)
    - [ Stop consumer on demand ](/docs/v2.8/advanced-usage/stopping-a-consumer)
    - [ Writing custom loggers ](/docs/v2.8/advanced-usage/custom-loggers)
    - [ Before and after callbacks ](/docs/v2.8/advanced-usage/before-callbacks)
    - [ Setting global configurations ](/docs/v2.8/advanced-usage/setting-global-configuration)
    - [ Sending multiple messages with the same producer ](/docs/v2.8/advanced-usage/sending-multiple-messages-with-the-same-producer)
- Testing
    -------

    - [ Kafka fake ](/docs/v2.8/testing/fake)
    - [ Assert Published ](/docs/v2.8/testing/assert-published)
    - [ Assert published On ](/docs/v2.8/testing/assert-published-on)
    - [ Assert nothing published ](/docs/v2.8/testing/assert-nothing-published)
    - [ Assert published times ](/docs/v2.8/testing/assert-published-times)
    - [ Assert published on times ](/docs/v2.8/testing/assert-published-on-times)
    - [ Mocking your kafka consumer ](/docs/v2.8/testing/mocking-your-kafka-consumer)

  Producing message batch to kafka 
==================================

 This feature is deprecated

 This is deprecated and will be removed in a future version. Please use [ async producers ](https://laravelkafka.com/docs/v2.0/producing-messages/producing-messages) instead of batch messaging. 

You can publish multiple messages at the same time by using message batches. To use a message batch, you must create a `Junges\Kafka\Producers\MessageBatch` instance. Then create as many messages as you want and push them to the `MesageBatch` instance. Finally, create your producer and call the `sendBatch`, passing the `MessageBatch` instance as a parameter. This is helpful when you persist messages in storage before publishing (e.g. TransactionalOutbox Pattern).

  Sponsorship 

Support Laravel Kafka by sponsoring me!

Laravel Kafka is free and Open Source software, built to empower developers like you. Your support helps maintain and enhance the project. If you find it valuable, please consider sponsoring me on GitHub. Every contribution makes a difference and keeps the development going strong! Thank you!

 [   Become a Sponsor ](https://github.com/sponsors/mateusjunges) Want to hide this message? Sponsor at any tier of $10/month or more! 

By using message batch, you can send multiple messages using the same producer instance, which is way faster than the default `send` method, which flushes the producer after each produced message. Messages are queued for asynchronous sending, and there is no guarantee that it will be sent immediately. The `sendBatch` is recommended for a system with high throughput.

         ```
use Junges\Kafka\Facades\Kafka;
use Junges\Kafka\Producers\MessageBatch;
use Junges\Kafka\Message\Message;

$message = new Message(
    headers: ['header-key' => 'header-value'],
    body: ['key' => 'value'],
    key: 'kafka key here',
    topicName: 'my_topic'
)

$messageBatch = new MessageBatch();
$messageBatch->push($message);
$messageBatch->push($message);
$messageBatch->push($message);
$messageBatch->push($message);

/** @var \Junges\Kafka\Producers\Builder $producer */
$producer = Kafka::publish('broker')
    ->onTopic('topic')
    ->withConfigOptions(['key' => 'value']);

$producer->sendBatch($messageBatch);
```

When producing batch messages, you can specify the topic for each message that you want to publish. If you want to publish all messages in the same topic, you can use the `onTopic` method on the `MessageBatch` class to specify the topic once for all messages. Please note that if a message within the batch specifies a topic, we will use that topic to publish the message, instead of the topic defined on the batch itself.

Previous

 [    Publishing to kafka ](https://laravelkafka.com/docs/v2.8/producing-messages/publishing-to-kafka) 

Next

 [ Creating a kafka consumer    ](https://laravelkafka.com/docs/v2.8/consuming-messages/creating-consumer) 

 Sponsors

 [ version="1.0" encoding="UTF-8"?       EasyCal ](https://easycal.app/) 

 [       Search  ⌘ K   ](https://typesense.org/)
