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
<PackageReference Include="NikosDelis.OpenSource.MbApifyer" Version="1.1.0" />
<PackageVersion Include="NikosDelis.OpenSource.MbApifyer" Version="1.1.0" />
<PackageReference Include="NikosDelis.OpenSource.MbApifyer" />
paket add NikosDelis.OpenSource.MbApifyer --version 1.1.0
#r "nuget: NikosDelis.OpenSource.MbApifyer, 1.1.0"
#:package NikosDelis.OpenSource.MbApifyer@1.1.0
#addin nuget:?package=NikosDelis.OpenSource.MbApifyer&version=1.1.0
#tool nuget:?package=NikosDelis.OpenSource.MbApifyer&version=1.1.0
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 | Versions 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. |
-
net8.0
- mqttnet (>= 4.3.3.952)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.