Add kafka when your app start.
// server.js
new Ignitor(fold)
.appRoot(__dirname)
// Only add the next line
.preLoad("start/kafka")
.fireHttpServer()
.catch(console.error);
Make sure to register the provider and make all of the following necessary changes inside the start/app.js
file!
// Add the kafka provider
const providers = [
// ...
"@frubana/adonis-kafka/providers/Provider"
];
Please update configuration before use. The configuration file is config/kafka.js
.
// start/kafka.js
const Kafka = use("Kafka");
// Callback function
Kafka.on("topic_name", (data, commit) => {
commit();
});
// Multiples topics with one controller.
Kafka.on("topic_name_1,topic_name_2,topic_name_3", (data, commit) => {
commit();
});
// Controller function
Kafka.on("topic_name", "TestController.index");
// app/Controllers/Kafka/TestController.js
/** @type {import('@adonisjs/framework/src/Logger')} */
const Logger = use("Logger");
class TestController {
index(data, commit) {
Logger.info("kafka data", data);
commit();
}
}
module.exports = TestController;
// app/Controllers/Http/TestController.js
const Kafka = use("Kafka");
class TestController {
somefunction() {
// data is a json object
const data = {};
Kafka.send("topicname", data);
}
}