KonstantinCodes / messenger-kafka

Simple Kafka transport for Symfony Messenger.
MIT License
84 stars 35 forks source link

fix(headers): remove headers from message json decode #16

Closed angelfs closed 4 years ago

angelfs commented 4 years ago

There is an error with this $decodeMessage['header'] because payload have not header.

KonstantinCodes commented 4 years ago

The headers field is very important because it specifies, which class the message belongs to and also the body content type.

Please just implement your own Decoder like so:

<?php

namespace App\Message;

use Koco\Kafka\Messenger\KafkaMessageDecoderInterface;
use RdKafka\Message;

class MyDecoder implements KafkaMessageDecoderInterface
{
    /**
     * @inheritDoc
     */
    public function decode(Message $message): array
    {
        return [
            'body' => $message->payload,
            'headers' => [
                'type' => TestMessage::class,
                'Content-Type' => 'application/json'
            ]
        ];
    }

    /**
     * @inheritDoc
     */
    public function supports(Message $message): bool
    {
        return $message->topic_name === 'test';
    }
}

You can put any logic in the decode method, to identify, which Class of Message it is.

And tag the service like so:

services:
    App\Message\MyDecoder:
        tags: ['koco.messenger_kafka.decoder']
KonstantinCodes commented 4 years ago

Added this documentation to https://github.com/KonstantinCodes/messenger-kafka#custom-message-formats

gennadigennadigennadi commented 4 years ago

@angelfs https://github.com/KonstantinCodes/messenger-kafka/pull/17 Thought this could be interesting for you.

KonstantinCodes commented 4 years ago

@angelfs New method is purely using the Symfony Serializer. That's better and more future proof. Documented here: https://github.com/KonstantinCodes/messenger-kafka#custom-message-formats