|
||
---|---|---|
.. | ||
lib/netstandard2.0 | ||
.signature.p7s | ||
Readme.md | ||
WebJobs.Extensions.MQTT.OutputBinding.1.0.4.nupkg |
Readme.md
%%%%%%
%%%%%%
@ %%%%%% @
@@ %%%%%% @@
@@@ %% 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!
How to use
Getting Started
- Create a custom configuration for the output binding by implementing the
ICustomConfigurationProvider
and defining your own MQTT client options. - Use the output binding attribute
[Mqtt]
with the custom configuration passing its type to attribute. For example, if your configuration class is namedMyCustomConfiguration
the attribute should be used like this:[Mqtt(typeof(MyCustomConfiguration))]
. - In your azure function you'll be able to publish a new message with a fully custom configurable MQTT client.
Custom Configuration Example
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 CustomCustomConfigurationProvider : 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 with output binding examples
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(CustomCustomConfigurationProvider))] IAsyncCollector<IMqttMessage> outMessages,
ILogger log)
{
await outMessages.AddAsync(
new MqttMessage(topic: "test/out", message: 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(CustomCustomConfigurationProvider))] out IMqttMessage outMessage,
ILogger log)
{
outMessage = new MqttMessage(topic: "test/out", message: Encoding.UTF8.GetBytes("hello"), qosLevel: MqttQualityOfServiceLevel.AtMostOnce, retain: false);
return new OkObjectResult("Message Enqueued!");
}
}
Please, see the examples in the sample project.