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

docs(examples): add multiple consumers usage #59

Merged
merged 2 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ fastify.listen(3000, err => {
console.log(`server listening on ${fastify.server.address().port}`)
})
```

For more examples on how to use this plugin you can take a look at the [examples directory](./examples).

### API
This module exposes the following APIs:
Expand Down
6 changes: 3 additions & 3 deletions example.js → examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ const fastify = require('fastify')({
}
})

const group = crypto.randomBytes(20).toString('hex')
const groupId = crypto.randomBytes(20).toString('hex')

fastify
.register(require('./'), {
.register(require('..'), {
producer: {
'metadata.broker.list': '127.0.0.1:9092',
dr_cb: true
},
consumer: {
'metadata.broker.list': '127.0.0.1:9092',
'group.id': group,
'group.id': groupId,
'fetch.wait.max.ms': 10,
'fetch.error.backoff.ms': 50
},
Expand Down
92 changes: 92 additions & 0 deletions examples/multiple-consumers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict'

const crypto = require('crypto')
const fastifyKafka = require('..')
const fastify = require('fastify')

const groupId = crypto.randomBytes(20).toString('hex')
const logger = { level: 'debug' }
const consumerOptions = {
consumer: {
'metadata.broker.list': '127.0.0.1:9092',
'group.id': groupId,
'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
})
})
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"scripts": {
"infra:start": "docker-compose up -d",
"infra:stop": "docker-compose stop",
"test": "standard && tsd && tap -j=1 test/*.js"
"lint": "standard",
"lint:fix": "standard --fix",
"test": "npm run lint && tap \"test/*.js\" && npm run test:typescript",
"test:typescript": "tsd"
},
"repository": {
"type": "git",
Expand Down