Skip to content

Latest commit

 

History

History
64 lines (59 loc) · 2.08 KB

README.md

File metadata and controls

64 lines (59 loc) · 2.08 KB

MQTTnet.EventBus

CI GitHub Nuget Nuget

Quick Start

In your ASP.NET Core Startup.cs file add the following

public void ConfigureServices(IServiceCollection services)
{
    //...

    var retryCount = 5;
    services.AddMqttEventBus(cfg =>
    {
        cfg
            .WithClientId("Api")
            .WithTcpServer("{Ip Address}", port: 1883);

    }, retryCount);
    services.AddTransient<MyEventHandler>();
}

An EventHandler is a class that may handle one or more message types. Each message type is defined by the IIntegrationEventHandler interface, where T is the MqttApplicationMessageReceivedEventArgs.

public class MyEventHandler : IIntegrationEventHandler
{
    public Task Handle(MqttApplicationMessageReceivedEventArgs args)
    {
        //Some action...
        return Task.CompletedTask;
    }
}

Then in your application add this extension

public static class ApplicationBuilderExtansions
{
    public static IApplicationBuilder UseEventBus(this IApplicationBuilder app, Action<IEventBus> action)
    {
        var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
        action.Invoke(eventBus);
        return app;
    }
}

and use it in your Startup.cs file

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //...

    app.UseEventBus(async bus => 
    {
        await bus.SubscribeAsync<IntegrationEventHandler>("MyTopic1");
    });
}

Injected interfaces

  • IEventBus
  • IMqttPersisterConnection
  • IEventBusSubscriptionsManager