Chd.Library.Kafka 8.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chd.Library.Kafka --version 8.0.7                
NuGet\Install-Package Chd.Library.Kafka -Version 8.0.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Chd.Library.Kafka" Version="8.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chd.Library.Kafka --version 8.0.7                
#r "nuget: Chd.Library.Kafka, 8.0.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Chd.Library.Kafka as a Cake Addin
#addin nuget:?package=Chd.Library.Kafka&version=8.0.7

// Install Chd.Library.Kafka as a Cake Tool
#tool nuget:?package=Chd.Library.Kafka&version=8.0.7                

Kafka library for .Net Core

Chd (cleverly handle difficulty) library helps you for stable and fast programming

📝 Table of Contents

🧐 About

Kafka is primarily used to build real-time streaming data pipelines and applications that adapt to the data streams.

🏁 Getting Started

Apache Kafka is an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications. This library simplfy Kafka using on .net core applications.

Prerequisites

You must use .net core 6.0 or higher

🎈 Usage

🔧 Injection Kafka Producer Into The Project

   var builder = WebApplication.CreateBuilder(args);
   ....
   ....
   builder.Services.AddKafkaProducer();

🔧 Producing Some Messages To Kafka

We will produce two messages for Kafka on different topics.

    public class KafkaTestController : ControllerBase
    {
        private readonly MessageProducer _mp;
        public KafkaTestController( MessageProducer mp)
        {
            this._mp = mp;
        }
        [Route("AddMessagesToKafkaQueue")]
        [HttpGet]
        public string? AddMessagesToKafkaQueue()//For testing on swagger or etc..
        {
            _mp.Produce("topic1", new EmailMessage { Subject = "Test1 Subject", Content = "Test1 Content", To = "test1@gmail.com" });
            Thread.Sleep(3000);
            _mp.Produce("topic2", new EmailMessage { Subject = "Test2 Subject", Content = "Test2 Content", To = "test2@gmail.com" });
            Thread.Sleep(3000);
            return "Ok";
        }
    }

Apsettings Configurations

You must add code below in appsettings.json

   "Kafka": {
    "Host": "192.168.1.1", //change ip
    "Port": "6501" //change port
  },

You can see the message in Kafka queue after running this code. Topics will be created automatically. We have come to the next stage, let's consume the messages. Open another solution for consume the produced messages. Let's say we have two consumers who consume different topics.

🔧 Consuming Messages

We will create two Kafka consumer service which inherits "ConsumerServiceBase" class

    public class Consumer1 : ConsumerServiceBase
    {
        public Consumer1()
        {
        }

        public override void OnMessageDelivered(string message)
        {
        //Add break point here
        }

        public override void OnErrorOccured(Error error)
        {
         //Add break point here
        }
    }
    public class Consumer2 : ConsumerServiceBase
    {
        public Consumer2()
        {
        }

        public override void OnMessageDelivered(string message)
        {
         //Add break point here
        }

        public override void OnErrorOccured(Error error)
        {
         //Add break point here
        }
    }

We should create two consumer service which consume different topics. We can now consume the messages we have previously produced.

🔧 Injection Kafka Consumer Into The Project

   var builder = WebApplication.CreateBuilder(args);
   ....
   ....
   builder.Services.AddKafkaConsumer<Consumer1>("topic1");
   builder.Services.AddKafkaConsumer<Consumer2>("topic2");

Now when we run the application we will consume the messages produced in kafka.

✍️ Authors

See also the list of contributors who participated in this project.

🎉 Acknowledgements

Thank you for using my library.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.6 66 1/30/2025
9.0.5 59 1/30/2025
9.0.4 77 1/19/2025
9.0.3 72 1/16/2025
9.0.2 92 12/30/2024
9.0.1 84 12/29/2024
9.0.0 84 12/25/2024
8.1.0 78 12/24/2024
8.0.7 84 12/23/2024
7.2.8 302 8/9/2023
1.2.8 189 4/26/2023