nestjs-kafka icon indicating copy to clipboard operation
nestjs-kafka copied to clipboard

Solution to Invalid Topic Name on Producer

Open samithbharadwaj opened this issue 3 years ago • 3 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @rob3000/[email protected] 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.

samithbharadwaj avatar Mar 30 '21 11:03 samithbharadwaj

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

rob3000 avatar Mar 30 '21 11:03 rob3000

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.

samithbharadwaj avatar Mar 31 '21 17:03 samithbharadwaj

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

rob3000 avatar Apr 01 '21 05:04 rob3000