nodejs-kafka
v1.0.0
Published
Library to make using kafka for Nodejs
Downloads
2
Readme
nodejs-kafka
This is a simple adapter for NodeJs to work with Kafka. Easy to initialize and use in any Framework.
Installation
npm i nodejs-kafka
Usage
Import library:
var kafka = require('nodejs-kafka') ;
This library provides two objects: KafkaProducer
, KafkaConsumer
Initialize a producer
Initialize a producer with [hostame:port]
, topic
, clientId
.
const producer1 = new kafka.KafkaProducer(["ip1:9092", "ip2:9092"],"topic1","client1")
Send a message:
producer1.send("my_message1", {a:"Hello world!", b:"I am vncafe!"})
Initialize a consumer
Initialize a producer with [hostame:port]
, topic
, clientId
. And a group name
to define a child room.
const consumer1 = new kafka.KafkaConsumer(["ip1:9092", "ip2:9092"],"topic1","client1", "group1")
Listen you message on "kafka-message"
:
consumer1.event.on("kafka-message", (key, message)=>{
console.log(key)
console.log(message)
})
Result:
my_message1
{a:"Hello world!", b:"I am vncafe!"}
Note
Do not create too many topics. It is recommended that the Kafka server should not have more than 100 topics. Therefore, distinguish the data streams by the key, or part of the message.
More infomation
Donate
ETH (erc20): 0x17e333b82011d8507654af5fcec467860299d607 BTC: 1FD3GgNxMXe1MeDic6Aa5MLW8FEggUAWhA