Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastify-kafka: having multiple consumers or producers #600

Closed
kibertoad opened this issue Jan 11, 2022 · 2 comments
Closed

fastify-kafka: having multiple consumers or producers #600

kibertoad opened this issue Jan 11, 2022 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@kibertoad
Copy link
Member

Is it possible to register more than one instance of consumer and/or producer?
Documentation for https://github.com/fastify/fastify-kafka only explains how to register one of each.

@kibertoad kibertoad added the help wanted Extra attention is needed label Jan 11, 2022
@0xvbetsun
Copy link

Sure, you can create multiple consumers. Probably, I should add this example

'use strict'

const crypto = require('crypto')
const fastifyKafka = require('fastify-kafka')
const fastify = require('fastify')

const group = crypto.randomBytes(20).toString('hex')
const logger = { level: 'debug' }
const consumerOptions = {
  consumer: {
    'metadata.broker.list': '127.0.0.1:9092',
    'group.id': group,
    'fetch.wait.max.ms': 10,
    'topic.metadata.refresh.interval.ms': 1000,
    'fetch.error.backoff.ms': 50
  },
  consumerTopicConf: {
    'auto.offset.reset': 'earliest'
  }
}

const producerOptions = {
  producer: {
    'metadata.broker.list': '127.0.0.1:9092',
    dr_cb: true
  }
}

const petConsumer = fastify({ logger })
  .register(fastifyKafka, consumerOptions)
  .after(err => {
    if (err) throw err

    petConsumer.kafka.subscribe(['cats', 'dogs'])
      .on('cats', (msg, commit) => {
        petConsumer.log.info('received message in cats topic: %s', msg.value.toString())
        commit()
      })
      .on('dogs', (msg, commit) => {
        petConsumer.log.info('received message in dogs topic: %s', msg.value.toString())
        commit()
      })

    petConsumer.kafka.consume()
  })

const plantConsumer = fastify({ logger })
  .register(fastifyKafka, consumerOptions)
  .after(err => {
    if (err) throw err

    plantConsumer.kafka.subscribe('flowers')
      .on('flowers', (msg, commit) => {
        plantConsumer.log.info('received message in flowers topic: %s', msg.value.toString())
        commit()
      })

    plantConsumer.kafka.consume()
  })

const producer = fastify({ logger })
  .register(fastifyKafka, producerOptions)
  .after(err => {
    if (err) throw err

    producer.kafka.push({
      topic: 'cats',
      payload: 'message about cats',
      key: 'testKey'
    })
    producer.kafka.push({
      topic: 'dogs',
      payload: 'message about dogs',
      key: 'testKey'
    })
    producer.kafka.push({
      topic: 'flowers',
      payload: 'message about flowers',
      key: 'testKey'
    })
  })

producer.ready(err => {
  if (err) throw err

  petConsumer.ready(err => {
    if (err) throw err
  })
  plantConsumer.ready(err => {
    if (err) throw err
  })
})

@kibertoad
Copy link
Member Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants