mdip / azure-webjobs-mqttoutput

A WebJobs extension for MQTT output binding

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %% MQTT %%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %

Microsoft Azure WebJobs MQTT Output binding for Azure Functions

A WebJobs extension for MQTT output binding based on MQTTnet library and the Managed Client extension.

This project is based on https://github.com/keesschollaart81/CaseOnline.Azure.WebJobs.Extensions.Mqtt.

The repository contains the code for the WebJobs.Extensions.MqttOutputBinding NuGet Package. This package enables you to publish a message to a MQTT topic as a result of an Azure Function.

Are you curious what MQTT is? Check this page!

GitHub license NuGet

How to use

Getting Started

  1. Create a custom configuration for the output binding by implementing the ICustomConfigurationProvider and defining your own MQTT client options.
  2. Use the output binding attribute [Mqtt] with the custom configuration passing its type to attribute. For example, if your configuration class is named MqttCustomConfigurationProvider the attribute usage should be used like this: [Mqtt(typeof(MqttCustomConfigurationProvider))].
  3. In your azure function you'll be able to publish a new message with a fully custom configurable MQTT client. See the examples for more.

Custom Configuration

ClientOptions property must not be null. The following example shows how to create a custom configuration. In this example a private static property has been used in order to build the configuration only once.

public class MqttCustomConfigurationProvider : ICustomConfigurationProvider
{
    private static readonly ManagedMqttClientOptions managedMqttClientOptions = BuildClientOptions();
    public ManagedMqttClientOptions ClientOptions => managedMqttClientOptions;

    private static ManagedMqttClientOptions BuildClientOptions()
    {
        ManagedMqttClientOptionsBuilder builder = new();
        MqttClientOptionsBuilder clientOptionsBuilder = new();
        clientOptionsBuilder
            .WithTcpServer("broker.hivemq.com",1883)
            .WithProtocolVersion(MqttProtocolVersion.V500)
            .WithClientId(Guid.NewGuid().ToString())
            .WithCredentials("user", "pass");
                
        builder
            .WithClientOptions(clientOptionsBuilder.Build())
            ;
        

        return builder.Build(); 
    }
}

Publish via output binding

Publishing messages on topic test/out.

public static class Example
{
    [FunctionName("AsyncCollector")]
    public static async Task<IActionResult> RunAsyncCollector(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "async-collector")] HttpRequest req, 
        [Mqtt(typeof(MqttCustomConfigurationProvider))] IAsyncCollector<IMqttMessage> outMessages, 
        ILogger log)
    {

        await outMessages.AddAsync(
            new MqttMessage(topic: "test/out", message: new ArraySegment<string>(Encoding.UTF8.GetBytes("hello")), qosLevel: MqttQualityOfServiceLevel.AtMostOnce, retain: false));

        return new OkObjectResult("Message Enqueued!");
    }
    
    [FunctionName("IMqttMessage")]
    public static IActionResult RunSingleMessage(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "mqtt-message")] HttpRequest req, 
        [Mqtt(typeof(MqttCustomConfigurationProvider))] out IMqttMessage outMessage,
        ILogger log)
    {
        outMessage = new MqttMessage(topic: "test/out", message: new ArraySegment<string>(Encoding.UTF8.GetBytes("hello")), qosLevel: MqttQualityOfServiceLevel.AtMostOnce, retain: false);
        
        return new OkObjectResult("Message Enqueued!");
    }
    
}

Please, see the examples in the sample project.

References

About

A WebJobs extension for MQTT output binding

License:MIT License


Languages

Language:C# 100.0%