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

@micro-dev-hub/kafka-event-sourcing

v1.0.2

Published

This is a package to support process publish and subscribe message. When We work with Kafka event driven.

Downloads

2

Readme

Module kafka-event-sourcing

Introduction

This module facilitates the transmission of messages to the Kafka system, seamlessly integrated with the Confluent Schema Registry. Leveraging Apache Avro schema, we register new schemas. After, We will follow up registered schema to encode, and decode messages, enhancing the security of messages transmitted to the Kafka system.

Getting started

  • Install package
npm install @micro-dev-hub/kafka-event-sourcing
# yarn add @micro-dev-hub/kafka-event-sourcing  
  • Create producer
// Example for publish messages
import { KafkaInstance } from "@micro-dev-hub/kafka-event-sourcing";

const clientId = "my-app";
const brokers = ["localhost:9092"];
const schemaRegistry = {host: "http://localhost:8081"};

const kafka = new KafkaInstance(clientId, brokers, schemaRegistry);

const producer = kafka.producer();

const produce = async() => {
    await producer.connect();
    let i = 1;
    let topicCount = 1;

    const schema = `
        {
            "type": "record",
            "name": "kafkaEventSourcingTest",
            "namespace": "examples",
            "fields": [{ "type": "string", "name": "fullName" }]
        }
        `;

    setInterval(async() => {
        try {
            if(topicCount > 10) {
                topicCount = 1;
            }

            await producer.send({
                topic: `topic-test-${topicCount}`,
                message: 
                    {
                        value: {fullName: `Test ${i} in topic-test-${topicCount}`}
                    }
            }, schema);

            console.log(`Test ${i} in topic-test-${topicCount}`);

            i++;
            topicCount++;
        } catch (error) {
            console.log(error);
        }
    }, 1000)
    
}

produce();
  • Create consumer
// Example for subcribe messages
import { EachMessagePayload } from "kafkajs";
import { IConsumerHandler, KafkaInstance } from "@micro-dev-hub/kafka-event-sourcing";

const clientId = "my-app";
const brokers = ["localhost:9092"];
const schemaRegistry = {host: "http://localhost:8081"};

const kafka = new KafkaInstance(clientId, brokers, schemaRegistry);

const consumer = kafka.consumer({
  groupId: 'group-1',
  minBytes: 5,
  maxBytes: 1e6,
  maxWaitTimeInMs: 3000,
});

consumer.connect();

const testhandler: IConsumerHandler[] = [
  {
    topics: ['topic-test-1','topic-test-2'],
    schemas: ['kafkaEventSourcingTest',],
    fromBeginning: true,
    handler: async (payload: IEachMessagePayload) => {
      console.log(`received ${payload.message.value} of topic-test-1 and topic-test-2`)
    }
  },
  {
    topics: ['topic-test-3',],
    schemas: [],
    fromBeginning: true,
    handler: async (payload: IEachMessagePayload) => {
      console.log(`received ${payload.message.value} of topic-test-3`)
    }
  },
  {
    topics: ['topic-test-4','topic-test-5'],
    schemas: [],
    fromBeginning: true,
    handler: async (payload: IEachMessagePayload) => {
      console.log(`received ${payload.message.value} of topic-test-4 and topic-test-5`)
    }
  },
]

consumer.reads({autoCommit: true},testhandler);

Features

KafkaInstance

  • Declare new KafkaInstance(clientId: string, brokers: string[], schemaRegistryAPIClientArgs: SchemaRegistryAPIClientArgs).

  • producer(): The method will return ProducerInstance.

  • consumer(consumerConfig: ConsumerConfig): The method will return ConsumerInstance.

ProducerInstance

  • connect(): The method will start connect to kafka.

  • disconnect(): The method will disconnect to kafka.

  • send(publishMessages: IPublishMessage, schema: string): The method facilitates schema registration with the Confluent Schema Registry. If the schema already exists, it returns the identifier of the existing schema; otherwise, it registers the new schema and returns its identifier. Subsequently, the method encodes the message using the registered schema and dispatches it to the Kafka system.

ConsumerInstance

  • connect(): The method will start connect to kafka.

  • disconnect(): The method will disconnect to kafka.

  • reads(consumerRunConfig: IConsumerRunConfig, consumerHandlers: IConsumerHandler[]): The reads method is designed to streamline the process of consuming messages from Kafka topics using the provided Kafka consumer configuration (consumerRunConfig) and an array of consumer handlers (consumerHandlers). The method iterates through each specified topic in the consumerHandlers, subscribes the consumer to those topics, and then initiates the consumer's execution. It allows can customize bussiness logic as user wish.