NikosDelis.OpenSource.MbApifyer 1.1.0

dotnet add package NikosDelis.OpenSource.MbApifyer --version 1.1.0
                    
NuGet\Install-Package NikosDelis.OpenSource.MbApifyer -Version 1.1.0
                    
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="NikosDelis.OpenSource.MbApifyer" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NikosDelis.OpenSource.MbApifyer" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="NikosDelis.OpenSource.MbApifyer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NikosDelis.OpenSource.MbApifyer --version 1.1.0
                    
#r "nuget: NikosDelis.OpenSource.MbApifyer, 1.1.0"
                    
#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.
#:package NikosDelis.OpenSource.MbApifyer@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NikosDelis.OpenSource.MbApifyer&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=NikosDelis.OpenSource.MbApifyer&version=1.1.0
                    
Install as a Cake Tool

Mb.APIfyer

In IoT, message based architecture is more than common and it's fantastic, right? Wouldn't it be great though, if you could get a response on your messages? Something like:

- Tell me your status
- My status is XXXXX

Now we can't really do that with message brokers. In best case we can get an Ack for our message. Mb.APIfyer tries to change exactly that! Mb.APIfyer (Mb is short for message broker) is a crazy little thing that wraps message brokers and makes them look and behave like APIs (or API-ish). Currently only MQTT is supported but others shouldn't be impossible to add. So instead of messages you send requests to endpoints (topics) with payloads (message body), and you get responses as well; just like any API.

Table of Contents

Installation

Nuget package available on nuget.org! The nuget package will be updated on every tag (major and minor versions get tagged).

Usage

Register with Dependency Injection and APIfy your Message Broker.

string myClientId = $"anonymous-client-{Guid.NewGuid()}";

// Let's APIfy an Mqtt-client
MqttApifyer ma = new MqttApifyer("127.0.0.1", myClientId);

var serviceProvider = new ServiceCollection()
    .AddSingleton<MbApifyer>(m => new MbApifyer(myClientId, ma))
    .BuildServiceProvider();

Put your code in a nice using-statement so that your connection get disposed when you don't need it any more.

using (var apifyer = serviceProvider.GetService<MbApifyer>())
{
    ...
}

If you are going to receive requests, then you need to <b>register your endpoints</b>. The APIfyer has already your clientId from the Constructor, just pass in an endpoint name and a Func (what will happen if you get a request to that endpoint) which gets and returns string.

await apifyer.RegisterEndpointAsync("ENDPOINT-NAME", (message) =>
{
    // Deserialize the message which comes in nice and serialized.
    // Do something
    
    string response = "something something something";
    return response;
});


You can send 2 types of request, either <b>fire & forget</b> or <b>fire & expect response</b>

// Fire & Forget (expects no response)
await apifyer.SendVoidRequestAsync("RECEIVER'S-CLIEND-ID", "ENDPOINT-NAME", new { Message = "Hello World" });

// Fire & expect response
string response = await apifyer.SendRequestAsync("RECEIVER'S-CLIEND-ID", "ENDPOINT-NAME", new { Message = "Hello World" });

Contributing

All feedback is welcome. This project is very young and it could all be a brain fart. If you have a good idea, or want to create unit tests or add another message broker (for whom I have an interface called IMessageBroker), then just make me a Pull Request and we take it from there!

License

This project is licensed under the MIT License.

Contact

Contact info available in my profile here.

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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.1.0 272 1/26/2024
1.0.0 202 1/19/2024