rob3000 / nestjs-kafka

NestJS integration with KafkaJS
The Unlicense
130 stars 44 forks source link

Solution to Invalid Topic Name on Producer #18

Open samithbharadwaj opened 3 years ago

samithbharadwaj commented 3 years ago

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch @rob3000/nestjs-kafka@1.3.0 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/@rob3000/nestjs-kafka/dist/kafka.service.js b/node_modules/@rob3000/nestjs-kafka/dist/kafka.service.js
index 9bd0b5b..0a13615 100644
--- a/node_modules/@rob3000/nestjs-kafka/dist/kafka.service.js
+++ b/node_modules/@rob3000/nestjs-kafka/dist/kafka.service.js
@@ -97,8 +97,8 @@ let KafkaService = KafkaService_1 = class KafkaService {
             this.logger.error('There is no producer, unable to send message.');
             return;
         }
-        const serializedPacket = await this.serializer.serialize(message);
-        return await this.producer.send(serializedPacket);
+        await Promise.all(message.messages.map(x => x.value = JSON.stringify(x.value)))
+        return await this.producer.send(message);
     }
     getGroupIdSuffix(groupId) {
         return groupId + '-client';

This issue body was partially generated by patch-package.

rob3000 commented 3 years ago

Hi @samithbharadwaj thanks for the diff. What config are you using? And are you using JSON for your messages in Kafka?

samithbharadwaj commented 3 years ago

Hi @rob3000

Are you referring to the Kafka Config? If yes, please find it below

{
        name: 'HERO_SERVICE',

        options: {
          client: {
            clientId: 'hero',
            brokers: ['localhost:9092'],
          },
          consumer: {
            groupId: 'hero-consumer',
          },
          producer: {
            allowAutoTopicCreation: true,
          },
        },
      },

Yes, I'm using JSON for my serialization and deserialization. I'm using Kafka for interservice communication between microservices.

PS: I'm sure there's a prettier/better way to do the serialization. This method would not hold up for circular JSON structures. But I'm just using this as a temporary fix. Will need to write a custom serializer very soon.

rob3000 commented 3 years ago

So most likely it looks like there could be an issue in the https://github.com/rob3000/nestjs-kafka/blob/master/src/serializer/kafka-request.serializer.ts