Replacing the default serializer/deserializer

The default serializer and deserializer are resolved from the service container, using the MessageSerializer and MessageDeserializer contracts. Out of the box, the JSON serializer and deserializer are used.

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 Want to hide this message? Sponsor at any tier of $10/month or more!

To replace them, bind the MessageSerializer and MessageDeserializer contracts to your own implementations, in the register method of a service provider:

$this->app->bind(\Junges\Kafka\Contracts\MessageSerializer::class, function () {
   return new MyCustomSerializer();
});

$this->app->bind(\Junges\Kafka\Contracts\MessageDeserializer::class, function() {
    return new MyCustomDeserializer();
});

To use a different serializer or deserializer for a single connection , for instance a cluster where messages are encoded with Avro, set the producer.serializer and consumer.deserializer keys of the connection instead:

'connections' => [
    'analytics' => [
        'brokers' => env('KAFKA_ANALYTICS_BROKERS'),
        'producer' => [
            'serializer' => \Junges\Kafka\Message\Serializers\AvroSerializer::class,
        ],
        'consumer' => [
            'deserializer' => \Junges\Kafka\Message\Deserializers\AvroDeserializer::class,
        ],
    ],
],

Both are resolved from the service container, so bind them with their dependencies, such as the schema registry, in a service provider.