Giter VIP home page Giter VIP logo

otc-pubsub's Introduction

Otc.PubSub

Build Status

Otc.PubSub goal is to abstract complex aspects of Publish/Subscribe systems by providing a comprehensive and easy to use API for .NET Standards 2.0.

Currently it supports only Apache Kafka as Publish/Subscribe backend. Otc.PubSub.Kafka was built on top of Confluent's .NET Client for Apache Kafka and provides Kafka implementation.

Quickstart

Installation

Recommended to install it from NuGet. It's spplited into two packages:

** Curretly only pre-release packages are available

Configuration

At startup, add IPubSub to your service collection by calling AddKafkaPubSub extension method for IServiceCollection (AddKafkaPubSub lives at Otc.PubSub.Kafka assembly):

services.AddKafkaPubSub(config => 
{
    config.Configure(new KafkaPubSubConfiguration() { BrokerList = "kafka-broker-addr-1,kafka-broker-addr-2 ..." };
});

AddKafkaPubSub will register KafkaPubSub implementation (from Otc.PubSub.Kafka assembly) for IPubSub interface (from Otc.PubSub.Abstractions assembly) as scoped lifetime.

Usage

Publish to a topic

IPubSub pubSub = ... // get pubSub from service provider (using dependency injection)

string message = "Hello world!";
byte[] messageBytes = Encoding.UTF8.GetBytes(message);

// Publish "Hello world!" string to a topic named "TopicName"
await pubSub.PublishAsync("TopicName", messageBytes);

Subscribe to topic(s)

Implement the interface IMessageHandler as your needs:

class MessageHandler : IMessageHandler
{
    public Task OnErrorAsync(object error, IMessage message)
    {
        // Handle errors while reading a message
    }

    public async Task OnMessageAsync(IMessage message)
    {
        // do something useful with the message
        ...
        
        // if the usuful operation above works fine, then commit message offset (mark it as read)
        await message.CommitAsync();
    }
}

Subscribe to topic(s) and start the process:

IPubSub pubSub = ... // taken from service provider

// Subscribe to "TopicName1" and "TopicName2" topics using "MyGroupId" as group identifier
ISubscription subscription = pubSub.Subscribe(new MessageHandler(), "MyGroupId", "TopicName1", "TopicName2", ...);

// Create a cancellation token source in order to be capable to stop process after start it.
CancellationTokenSource cts = new CancellationTokenSource();

// Start the process
await subscription.StartAsync(cts.Token);

otc-pubsub's People

Contributors

matheusneder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.