npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kafka-pipeline

v3.2.0

Published

A robust, easy to use kafka consumer

Downloads

16

Readme

kafka-pipeline

This library provide a robust, easy to use kafka message consumer based on ConsumerGroup from kafka-node that helps you managing message offsets in correct way.

Design notes

The basic idea behind this library is separate consume progress and offset commit into two Transform stream, namely ConsumeStream and CommitStream, messages will be consumed by ConsumeStream by invoking a specified callback within a specified concurrency limit. And, ConsumeStream will yield all received messages in the same order to CommitStream when all the condition following are met:

  1. the message itself has been consumed

  2. all predecessor message in the same partition have been consumed

While the CommitStream remember the latest offset of each partitions and topics, commit them repeatedly in a specified interval as well as the time when the consumer is rebalanced or closed.

While this idea is originate from ConsumerGroupStream from kafka-node, it's buggy when you try to use it with offset being managed by yourself, which prevents you from implementing a reliable message consumer meets guarantee that each message should be consumed at least once.

Even if you don't care about at-least-once guarantee at all, ConsumerGroupStream duplicates a lot of message after rebalance. Although your apps need to be idempotency when handle message, this library take the best efforts to minimize the duplication of messages, unless error occurred that prevent the offset to be committed, it shall NEVER duplicate any message.

Installation

kafka-node is A peer dependency of this library, you need to install it together with kafka-pipeline.

npm install kafka-pipeline kafka-node

Usage

const {ConsumerGroupPipeline} = require('kafka-pipeline');

const consumerPipeline = new ConsumerGroupPipeline({
  topic: TOPIC_NAME,
  messageConsumer: async (message) => {
    //...
  },
  // A optional callback function handled messages failed to consume by
  // `messageConsumer`. When error raised in `messageConsumer`,
  // consumerPipeline will be closed if you don't provide `failedMessageConsumer`,
  // or `failedMessageConsumer` also raise an exception
  failedMessageConsumer: async (exception, message) => {
    //...
  },
  consumeConcurrency: 8,
  consumeTimeout: 5000,
  commitInterval: 10000,
  consumerGroupOption: {
    groupId: GROUP_ID
    // Properties of this object will be pass to ConsumerGroup
    // except that some of them could be override by ConsumerGroupPipeline
    // See reference of its setting at https://github.com/SOHU-Co/kafka-node
    //
  }
});

consumerPipeline.start();

// The following code is for demo purpose only, not suggesting you to do this
// in production code. But to avoid message duplication you need to ensure your
// app will quit in a graceful way that await the promise returned by
// consumerPipeline.close() is resolved, which will do the final offset commit
process.on('SIGTERM', function () {
  consumerPipeline.close().then(function () {
    process.exit(0);
  });
});

Breaking changes

  • 3.0 (Upcoming): consumeConcurrency now limits message consumed concurrently per-partition, not per-consumer, default value changed to 1

  • 2.0: Migrated to Typescript

License

ISC License, see LICENSE file.

TODO

  1. More test
  2. Consider support HighLevelConsumer?