Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
kafka-node
Advanced tools
The kafka-node package is a client library for Apache Kafka, a distributed streaming platform. It allows you to produce and consume messages, manage Kafka topics, and handle Kafka streams in Node.js applications.
Producer
This feature allows you to produce messages to a Kafka topic. The code sample demonstrates how to create a Kafka producer, connect to a Kafka broker, and send a message to a specified topic.
const kafka = require('kafka-node');
const client = new kafka.KafkaClient({kafkaHost: 'localhost:9092'});
const producer = new kafka.Producer(client);
const payloads = [
{ topic: 'topic1', messages: 'hello world', partition: 0 }
];
producer.on('ready', function () {
producer.send(payloads, function (err, data) {
console.log(data);
});
});
producer.on('error', function (err) {
console.error('Producer error:', err);
});
Consumer
This feature allows you to consume messages from a Kafka topic. The code sample demonstrates how to create a Kafka consumer, connect to a Kafka broker, and listen for messages on a specified topic.
const kafka = require('kafka-node');
const client = new kafka.KafkaClient({kafkaHost: 'localhost:9092'});
const consumer = new kafka.Consumer(
client,
[
{ topic: 'topic1', partition: 0 }
],
{
autoCommit: true
}
);
consumer.on('message', function (message) {
console.log(message);
});
consumer.on('error', function (err) {
console.error('Consumer error:', err);
});
Admin
This feature allows you to perform administrative tasks such as listing topics. The code sample demonstrates how to create an admin client and list all topics in the Kafka cluster.
const kafka = require('kafka-node');
const client = new kafka.KafkaClient({kafkaHost: 'localhost:9092'});
const admin = new kafka.Admin(client);
admin.listTopics((err, res) => {
console.log(res);
});
KafkaJS is a modern Apache Kafka client for Node.js. It is fully written in JavaScript and provides a more idiomatic and modern API compared to kafka-node. KafkaJS is known for its simplicity, better documentation, and active maintenance.
node-rdkafka is a high-performance Node.js client for Apache Kafka based on the C/C++ library librdkafka. It offers more advanced features and better performance compared to kafka-node, but it requires native module compilation.
Kafka-node is a nodejs client with zookeeper integration for apache Kafka, only support the latest version of kafka 0.8 which is still under development, so this module
is not production ready
so far.
Zookeeper does the following jobs:
Follow the instructions on the Kafka wiki to build Kafka 0.8 and get a test broker up and running.
connectionString
: zookeeper connection string, default localhost:2181/kafka0.8
clientId
: This is a user supplied identifier for the client application, default kafka-node-client
client
: client which keep connect with kafka server.var kafka = require('kafka-node'),
Producer = kafka.Producer,
client = new kafka.Client(),
producer = new Producer(client);
payloads
: Array,array of ProduceRequest
, ProduceRequest
is a JSON object like:{
topic: 'topicName',
messages: ['message body'],// multi messages should be a array, single message can be just a string
partition: '0', //default 0
}
cb
: Function, the callbackExample:
var kafka = require('kafka-node'),
Producer = kafka.Producer,
client = new kafka.Client(),
producer = new Producer(client),
payloads = [
{ topic: 'topic1', messages: 'hi', partition: 0 },
{ topic: 'topic2', messages: ['hello', 'world'] }
];
producer.on('ready', function () {
producer.send(payloads, function (err, data) {
console.log(data);
});
})
This method is used to create topics in kafka server, only work when kafka server set auto.create.topics.enable
true, our client simply send a metadata request to let server auto crate topics. when async
set false, this method does not return util all topics are created, otherwise return immediately.
topics
: Array,array of topicsasync
: Boolean,async or synccb
: Function,the callbackExample:
var kafka = require('kafka-node'),
Producer = kafka.Producer,
client = new kafka.Client(),
producer = new Producer(client);
// Create topics sync
producer.createTopics(['t','t1'], false, function (err, data) {
console.log(data);
});
// Create topics async
producer.createTopics(['t'], true, function (err, data) {});
producer.createTopics(['t'], function (err, data) {});// Simply omit 2nd arg
client
: client which keep connect with kafka server.payloads
: Array,array of FetchRequest
, FetchRequest
is a JSON object like:{
topic: 'topicName',
partition: '0', //default 0
offset: 0, //default 0
}
options
: options for consumer,{
groupId: 'kafka-node-group',//consumer group id, deafult `kafka-node-group`
// Auto commit config
autoCommit: true,
autoCommitIntervalMs: 5000,
// The max wait time is the maximum amount of time in milliseconds to block waiting if insufficient data is available at the time the request is issued, default 100ms
fetchMaxWaitMs: 100,
// This is the minimum number of bytes of messages that must be available to give a response, default 1 byte
fetchMinBytes: 1,
// The maximum bytes to include in the message set for this partition. This helps bound the size of the response.
fetchMaxBytes: 1024 * 10,
// If set true, consumer will fetch message from the given offset in the payloads
fromOffset: false
}
Example:
var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.Client(),
consumer = new Consumer(
client,
[
{ topic: 't', partition: 0 }, { topic: 't1', partition: 1 }
],
{
autoCommit: false
}
);
By default, we will consume message from the last committed offset of the current group
onMessage
: Function, callback when new message comesExample:
consumer.on('message', function (message) {
console.log(message);
});
Add topics to current consumer, if any topic to be added not exists, return error
topics
: Array, array of topics to addcb
: Function,the callbackExample:
consumer.addTopics(['t1', 't2'], function (err, added) {
});
topics
: Array, array of topics to removecb
: Function, the callbackExample:
consumer.removeTopics(['t1', 't2'], function (err, removed) {
});
Commit offset of the current topics manually, this method should be called when a consumer leaves
cb
: Function, the callbackExample:
consumer.commit(function(err, data) {
});
Set offset of the given topic
topic
: String
partition
: Number
offset
: Number
Example:
consumer.setOffset('topic', 0, 0);
force
: Boolean, if set true, it force commit current offset before close, default falseExample
consumer.close(true);
client
: client which keep connect with kafka server.payloads
: Array,array of OffsetRequest
, OffsetRequest
is a JSON object like:{
topic: 'topicName',
partition: '0', //default 0
time: Date.now(), //used to ask for all messages before a certain time (ms), not support negative,default Date.now()
maxNum: 1 //default 1
}
cb
: Function, the callbackExample
var kafka = require('kafka'),
client = new kafka.Client(),
offset = new kafka.Offset(client);
offset.fetch([
{ tiopic: 't', partition: 0, time: Date.now(), maxNum: 1 }
], function (err, data) {
// data
// { 't': { '0': [999] } }
});
groupId
: consumer grouppayloads
: Array,array of OffsetCommitRequest
, OffsetCommitRequest
is a JSON object like:{
topic: 'topicName',
partition: '0', //default 0
offset: 1,
metadata: 'm', //default 'm'
}
Example
var kafka = require('kafka-node'),
client = new kafka.Client(),
offset = new kafka.Offset(client);
offset.commit('groupId', [
{ tiopic: 't', partition: 0, offset: 10 }
], function (err, data) {
});
Copyright (c) 2013 Sohu.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Client for Apache Kafka v0.9.x, v0.10.x and v0.11.x
The npm package kafka-node receives a total of 133,078 weekly downloads. As such, kafka-node popularity was classified as popular.
We found that kafka-node demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.