Custom serializers | 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/v1.13/introduction)
    - [ Requirements ](/docs/v1.13/requirements)
    - [ Installation and Setup ](/docs/v1.13/installation-and-setup)
    - [ Questions and issues ](/docs/v1.13/questions-and-issues)
    - [ Changelog ](/docs/v1.13/changelog)
    - [ Upgrade Guide ](/docs/v1.13/upgrade-guide)
- Producing messages
    ------------------

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

    - [ Creating a kafka consumer ](/docs/v1.13/consuming-messages/creating-consumer)
    - [ Subscribing to kafka topics ](/docs/v1.13/consuming-messages/subscribing-to-kafka-topics)
    - [ Consumer groups ](/docs/v1.13/consuming-messages/consumer-groups)
    - [ Message handlers ](/docs/v1.13/consuming-messages/message-handlers)
    - [ Configuring consumer options ](/docs/v1.13/consuming-messages/configuring-consumer-options)
    - [ Custom deserializers ](/docs/v1.13/consuming-messages/custom-deserializers)
    - [ Consuming messages ](/docs/v1.13/consuming-messages/consuming-messages)
    - [ Handling message batch ](/docs/v1.13/consuming-messages/handling-message-batch)
    - [ Class structure ](/docs/v1.13/consuming-messages/class-structure)
- Advanced usage
    --------------

    - [ Replacing the default serializer/deserializer ](/docs/v1.13/advanced-usage/replacing-default-serializer)
    - [ Graceful shutdown ](/docs/v1.13/advanced-usage/graceful-shutdown)
    - [ SASL Authentication ](/docs/v1.13/advanced-usage/sasl-authentication)
    - [ Custom Committers ](/docs/v1.13/advanced-usage/custom-committers)
    - [ Middlewares ](/docs/v1.13/advanced-usage/middlewares)
    - [ Stop consumer after last messages ](/docs/v1.13/advanced-usage/stop-consumer-after-last-message)
    - [ Writing custom loggers ](/docs/v1.13/advanced-usage/custom-loggers)
    - [ Setting global configurations ](/docs/v1.13/advanced-usage/setting-global-configuration)
- Testing
    -------

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

  Custom serializers 
====================

Serialization is the process of converting messages to bytes. Deserialization is the inverse process - converting a stream of bytes into and object. In a nutshell, it transforms the content into readable and interpretable information.

Basically, in order to prepare the message for transmission from the producer we use serializers. This package supports three serializers out of the box:

- NullSerializer / NullDeserializer
- JsonSerializer / JsonDeserializer
- AvroSerializer / JsonDeserializer

If the default `JsonSerializer` does not fulfill your needs, you can make use of custom serializers.

To create a custom serializer, you need to create a class that implements the `\Junges\Kafka\Contracts\MessageSerializer` contract. This interface force you to declare the serialize method.

You can inform your producer which serializer should be used with the `usingSerializer` method:

         ```
$producer = \Junges\Kafka\Facades\Kafka::publishOn('topic')->usingSerializer(new MyCustomSerializer());
```

To create a custom serializer, you need to create a class that implements the `\Junges\Kafka\Contracts\MessageSerializer` contract. This interface force you to declare the `serialize` method.

### [](#content-using-avro-serializer "Permalink")Using AVRO serializer

To use the AVRO serializer, add the AVRO serializer:

         ```
use FlixTech\AvroSerializer\Objects\RecordSerializer;
use FlixTech\SchemaRegistryApi\Registry\CachedRegistry;
use FlixTech\SchemaRegistryApi\Registry\BlockingRegistry;
use FlixTech\SchemaRegistryApi\Registry\PromisingRegistry;
use FlixTech\SchemaRegistryApi\Registry\Cache\AvroObjectCacheAdapter;
use GuzzleHttp\Client;

$cachedRegistry = new CachedRegistry(
    new BlockingRegistry(
        new PromisingRegistry(
            new Client(['base_uri' => 'kafka-schema-registry:9081'])
        )
    ),
    new AvroObjectCacheAdapter()
);

$registry = new AvroSchemaRegistry($cachedRegistry);
$recordSerializer = new RecordSerializer($cachedRegistry);

//if no version is defined, latest version will be used
//if no schema definition is defined, the appropriate version will be fetched form the registry
$registry->addBodySchemaMappingForTopic(
    'test-topic',
    new \Junges\Kafka\Message\KafkaAvroSchema('bodySchemaName' /*, int $version, AvroSchema $definition */)
);
$registry->addKeySchemaMappingForTopic(
    'test-topic',
    new \Junges\Kafka\Message\KafkaAvroSchema('keySchemaName' /*, int $version, AvroSchema $definition */)
);

$serializer = new \Junges\Kafka\Message\Serializers\AvroSerializer($registry, $recordSerializer /*, AvroEncoderInterface::ENCODE_BODY */);

$producer = \Junges\Kafka\Facades\Kafka::publishOn('topic')->usingSerializer($serializer);
```

Previous

 [    Configuring message payload ](https://laravelkafka.com/docs/v1.13/producing-messages/configuring-message-payload) 

Next

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

 Sponsors

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

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