kafka-observe
This library is a wrapper around kafkajs. kafka-observe allows for kafka topics to be transformed in rxjs Subjects. This way you can listen to events on kafka by subscribing to the topics instead of setting up the handling of streams yourself.
This library is very opinionated and supports our way of working at Bothive. However it works great for us and is very versatile so it might be the way you prefer working as wel!
Note: We have introduced braking changes in 2.1.0. 2.1.0 is NOT compatible with previous versions because payloads look differend. Make sure to upgrade all your kafka services at once if you upgrade from previous versions.
usage
This library exports a function with one parameter options
to initialise the library.
Note: We have used the singleton patern to make sure that you don't create multiple connections and make sure you don't waste CPU and RAM on multiple connections. Only the first options passed will be applied. The rest will be ignored and pass the existing instance.
Require the library as follows:
const kafka = require("kafka-observe")(options);
The option object takes the following parameters:
const options = {
kafkaHost: "kafka host and port"
topicsToFollow: [
{
topic: "name of the kafka topic",
strategy: "All|LoadBalanced|Both",
},
...
],
enableConsumer: true,
enableConsumerGroup: true,
enableProducer: true,
consumerGroupId: "consumer group name",
consumerId: "consumer name",
liveness: {
topic: "liveness",
interval: 2000,
event: "ALIVE_TICK",
},
logAllEvents: "false",
ssl,
sasl,
},
Passing these options will return an object set up to communicate with your kafka cluster. After initialisation the kafka object will have these properties and functions:
consumer
: a connected consumer instance
consumerGroup
: a connected consumer group instance
producer
: instance of a connected producer
getTopicSubject
: a function that returns a topic to subscribe to with the correct strategy.
eventCallback
: a function that sends out an event and listens for a response
getTopicSubject (recommended method)
Returns a subject you can subscribe to listen to events of a certain topic.
const { getTopicSubject } = require("kafka-observe")(options);
getTopicSubject({
topic: "topic name",
loadBalanced: "true|false",
}).subscribe((event) => {
});
eventCallback
Returns a subject you can subscribe to listen to events of a certain topic.
const { eventCallback } = require("kafka-observe")(options);
eventCallback({
topic: "topic name",
sender: "event",
listeners: ["listen_for_event"],
payload: {}
}).then((event) => {
});
writeStream
Produces a large object in streams over a topic
const { writeStream } = require("kafka-observe")(options);
writeStream({
topic: "topic name",
headers: {},
event: "listen_for_event",
limit: 2000000,
payload: {}
}).then((event) => {
});
readStream
Listens to a write stream and returns the complete object on finish
const { readStream } = require("kafka-observe")(options);
readStream({
topic: "topic name",
events: ["listen_for_event"],
filter: () => true,
onStatus: (status) => console.log(status)
}).then((event) => {
});
streamCallback
Sends out an event and listens to the stream event
const { readStream } = require("kafka-observe")(options);
readStream({
topic: "topic name",
sender: "event",
listeners: ["listen_for_event"],
payload: {}
onStatus: (status) => console.log(status)
}).then((event) => {
});
producer
Use this exported class to produce messages to kafka on any topic you want (no need to put the topic in the topicsToFollow if you only want to produce messages to that topic)
const { producer } = require("kafka-observe")(options);
producer.sendMessage({
topic: "name of the topic",
payload: {},
headers: {},
})
consumer & consumerGroup
If you want you can also get the topic subject directly from the comsumer or consumerGrup instances.
Consumer handles all the events with the All and Both strategy.
The Consumer group handles all Loadbalanced and Both strategy events.
const { consumer, consumerGroup } = require("kafka-observe")(options);
consumer.getTopicSubject("topic name")
.subscribe((event) => {
});
OR
consumerGroup.getTopicSubject("topic name")
.subscribe((event) => {
});
creators
This project is created and used by the Bothive team to streamline our kafka process. We used to have the same exact code in all our services and we created this library to unify our kafka code in an independent library. Feel free to use and modify as you like. We will continue to update this library as long as we use it ourselves.