Giter VIP home page Giter VIP logo

mb.apifyer's Introduction

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 register your endpoints. 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 fire & forget or fire & expect response

// 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.

mb.apifyer's People

Contributors

nikos-rebl avatar

Stargazers

 avatar  avatar  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.