Connections | 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/v3.0/introduction)
    - [ Requirements ](/docs/v3.0/requirements)
    - [ Installation and Setup ](/docs/v3.0/installation-and-setup)
    - [ Questions and issues ](/docs/v3.0/questions-and-issues)
    - [ Changelog ](/docs/v3.0/changelog)
    - [ Upgrade guide ](/docs/v3.0/upgrade-guide)
    - [ Example docker-compose file ](/docs/v3.0/example-docker-compose)
- Producing messages
    ------------------

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

    - [ Creating a kafka consumer ](/docs/v3.0/consuming-messages/creating-consumer)
    - [ Subscribing to kafka topics ](/docs/v3.0/consuming-messages/subscribing-to-kafka-topics)
    - [ Using regex to subscribe to kafka topics ](/docs/v3.0/consuming-messages/using-regex-to-subscribe-to-kafka-topics)
    - [ Assigning consumers to a topic partition ](/docs/v3.0/consuming-messages/assigning-partitions)
    - [ Consuming messages from specific offsets ](/docs/v3.0/consuming-messages/consuming-from-specific-offsets)
    - [ Consumer groups ](/docs/v3.0/consuming-messages/consumer-groups)
    - [ Partition Discovery and Dynamic Assignment ](/docs/v3.0/consuming-messages/partition-discovery)
    - [ Message handlers ](/docs/v3.0/consuming-messages/message-handlers)
    - [ Configuring consumer options ](/docs/v3.0/consuming-messages/configuring-consumer-options)
    - [ Handling failed messages ](/docs/v3.0/consuming-messages/handling-failed-messages)
    - [ Custom deserializers ](/docs/v3.0/consuming-messages/custom-deserializers)
    - [ Consuming messages ](/docs/v3.0/consuming-messages/consuming-messages)
    - [ Pausing partitions ](/docs/v3.0/consuming-messages/pausing-partitions)
    - [ Consumer classes ](/docs/v3.0/consuming-messages/class-structure)
    - [ Using consumers with Laravel Telescope ](/docs/v3.0/consuming-messages/laravel-telescope)
- Advanced usage
    --------------

    - [ Connections ](/docs/v3.0/advanced-usage/connections)
    - [ Replacing the default serializer/deserializer ](/docs/v3.0/advanced-usage/replacing-default-serializer)
    - [ Graceful shutdown ](/docs/v3.0/advanced-usage/graceful-shutdown)
    - [ Running consumers in production ](/docs/v3.0/advanced-usage/running-consumers-in-production)
    - [ SASL Authentication ](/docs/v3.0/advanced-usage/sasl-authentication)
    - [ Custom Committers ](/docs/v3.0/advanced-usage/custom-committers)
    - [ Manual Commit ](/docs/v3.0/advanced-usage/manual-commit)
    - [ Middlewares ](/docs/v3.0/advanced-usage/middlewares)
    - [ Stop consumer when there are no messages left ](/docs/v3.0/advanced-usage/stop-consumer-after-last-message)
    - [ Stop consumer on demand ](/docs/v3.0/advanced-usage/stopping-a-consumer)
    - [ Writing custom loggers ](/docs/v3.0/advanced-usage/custom-loggers)
    - [ Before and after callbacks ](/docs/v3.0/advanced-usage/before-callbacks)
    - [ Setting global configurations ](/docs/v3.0/advanced-usage/setting-global-configuration)
    - [ Sending multiple messages with the same producer ](/docs/v3.0/advanced-usage/sending-multiple-messages-with-the-same-producer)
    - [ Events ](/docs/v3.0/advanced-usage/events)
    - [ Consumer lag ](/docs/v3.0/advanced-usage/consumer-lag)
- Testing
    -------

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

  Connections 
=============

A connection holds everything needed to talk to a Kafka cluster: the brokers, the authentication settings and the librdkafka options used by producers and consumers. Connections are defined in the `connections` key of your `config/kafka.php` file, and the `default` key defines which one is used when you don't specify one.

         ```
'default' => env('KAFKA_CONNECTION', 'default'),

'connections' => [
    'default' => [
        'brokers' => env('KAFKA_BROKERS', 'localhost:9092'),
        'security_protocol' => env('KAFKA_SECURITY_PROTOCOL', 'PLAINTEXT'),
        // ...
    ],

    'analytics' => [
        'brokers' => env('KAFKA_ANALYTICS_BROKERS'),
        'security_protocol' => 'SASL_SSL',
        'sasl' => [
            'mechanism' => 'SCRAM-SHA-512',
            'username' => env('KAFKA_ANALYTICS_USERNAME'),
            'password' => env('KAFKA_ANALYTICS_PASSWORD'),
        ],
        'producer' => [
            'options' => ['enable.idempotence' => true],
        ],
        'consumer' => [
            'group_id' => 'analytics',
        ],
    ],
],
```

  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! 

### [](#content-using-a-connection "Permalink")Using a connection

The `publish`, `publishSync` and `consumer` methods of the `Kafka` facade use the default connection. To use another one, call them on the connection returned by the `connection` method:

         ```
use Junges\Kafka\Facades\Kafka;

Kafka::connection('analytics')->publish('page-views')->withBody(['path' => '/'])->send();

Kafka::connection('analytics')->consumer(['page-views'])
    ->withHandler(new PageViewHandler)
    ->build()
    ->consume();
```

[ Consumer classes ](/consuming-messages/class-structure) use the connection returned by their `connection` method.

### [](#content-configuration-reference "Permalink")Configuration reference

KeyDescription`brokers`A comma separated list of brokers.`security_protocol`The security protocol: `PLAINTEXT`, `SSL`, `SASL_PLAINTEXT` or `SASL_SSL`.`sasl`The SASL `mechanism`, `username` and `password`. SASL is used when a username is set, with the `SASL_SSL` protocol when `security_protocol` is `SSL` or `SASL_SSL`, and `SASL_PLAINTEXT` otherwise.`options`librdkafka options applied to both producers and consumers.`producer.options`librdkafka options applied only to the producer.`producer.serializer`The class serializing the published messages, resolved from the service container. Defaults to the `MessageSerializer` binding, the JSON serializer.`producer.flush_timeout_ms`How long to wait for queued messages to be delivered when flushing the producer.`producer.flush_retries`How many times to retry flushing before giving up.`producer.flush_retry_sleep_ms`How long to wait between flush retries.`consumer.group_id`The default consumer group id.`consumer.auto_commit`Whether consumers commit offsets automatically after handling each message.`consumer.timeout_ms`How long the consumer waits for a message before polling again.`consumer.options`librdkafka options applied only to consumers.`consumer.deserializer`The class deserializing the consumed messages, resolved from the service container. Defaults to the `MessageDeserializer` binding, the JSON deserializer.The consumer builder methods, such as `withSasl`, `withOptions` and `withAutoCommit`, can still be used to override the connection configuration for a single consumer.

Previous

 [    Using consumers with Laravel Telescope ](https://laravelkafka.com/docs/v3.0/consuming-messages/laravel-telescope) 

Next

 [ Replacing the default serializer/deserializer    ](https://laravelkafka.com/docs/v3.0/advanced-usage/replacing-default-serializer) 

 Sponsors

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

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