Skip to content

phambanhan/egg-apache-kafkajs

Repository files navigation

egg-apache-kafkajs

kafkajs plugin for Egg.js.

NOTE: This plugin just for integrate kafkajs into Egg.js, more documentation please visit https://kafka.js.org/docs/getting-started.

Install

$ npm i egg-apache-kafkajs --save

or

$ yarn add egg-apache-kafkajs

Configuration

// {app_root}/config/config.default.js
exports.kafka = {
  options: {
    clientId: 'clientId',
    brokers: [ 'broker:9092' ],
    connectionTimeout: 3000,
  },
  consumers: [
    {
      groupId: 'consumer-groupId',
      topics: [ 'topic1' ],
    },
  ],
};
// {app_root}/config/plugin.js
exports.kafka = {
  enable: true,
  package: 'egg-apache-kafkajs',
};

Usage

// Producer
const kafka = this.ctx.app.kafka;
await kafka.send({
  topic: 'topic1',
  messages: [{
    value: 'Hello World',
  }],
});
// Producer
const kafka = this.ctx.app.kafka;
const producer = kafka.producer();
await producer.connect();
await producer.send({
  topic: 'topic1',
  messages: [{
    value: 'Hello World',
  }],
});

License

MIT

Releases

No releases published

Packages

No packages published