@kafka-ts/fastify-producer
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Fastify's Plugin for Kafka Producer.

Install

npm install --save @kafka-ts/fastify-producer

# or

yarn add @kafka-ts/fastify-producer

# or

pnpm add @kafka-ts/fastify-producer

Usage

import detect from 'detect-port';
import KafkaProducer from '@kafka-ts/fastify-producer';

import { fastify } from 'config/fastify';

async function bootstrap(): Promise<typeof fastify> {
  fastify.register(KafkaProducer, {
    brokers: ['localhost:9092'],
  });

  fastify.get('/', async (_request, reply) => {
    const result = await fastify.kafkaProducer.publish({
      topicMessages: {
        topic: 'topic',
        messages: [
          {
            value: 'hello from producer',
          },
        ],
      },
    });

    console.log(result);

    reply.send('Ok');
  });

  fastify.get('/message', async (_request, reply) => {
    const result = await fastify.kafkaProducer.publish({
      topicMessages: {
        topic: 'topic_2',
        messages: [
          {
            value: 'hello from producer',
          },
        ],
      },
    });

    console.log(result);

    reply.send('Ok');
  });

  const port = await detect(3_000);
  await fastify.listen({
    port,
  });

  return fastify;
}

bootstrap();

Package Sidebar

Install

npm i @kafka-ts/fastify-producer

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

9.29 kB

Total Files

7

Last publish

Collaborators

  • zgid123