Publish via output

Kees Schollaart edited this page Apr 16, 2018 · 7 revisions

Output binding

  • Add the MqttAttribute to one of your functions parameters
  • Use one of the constructor overloads to configure
    • The first/simple constructor which does not require any parameter
    • The second (more advanced) constructor requires a Type which you then have to implement more on this
  • Make sure the type of the parameter is IMqttMessage and marked as out

Example

This is an example of an Azure Function running based on a timer trigger publishing messages to a MQTT topic:

[FunctionName("SimpleFunction")]
public static void SimpleFunction(
    [TimerTrigger("0 */5 * * * *")]TimerInfo timer,
    [Mqtt] out IMqttMessage outMessage)
{
    outMessage = new MqttMessage("testtopic/out", new byte[] { }, MqttQualityOfServiceLevel.AtLeastOnce, true);
}

It is also possible to output multiple messages in a single functions run using the ICollector output:

[FunctionName("TimerFunction")]
public static void TimerFunction( 
    [TimerTrigger("0 * * * * *")]TimerInfo timerInfo,
    [Mqtt] ICollector<IMqttMessage> outMessages)
{
    outMessages.Add(new MqttMessage("topic/one", body, MqttQualityOfServiceLevel.AtLeastOnce, true));
    outMessages.Add(new MqttMessage("topic/two", body, MqttQualityOfServiceLevel.AtLeastOnce, true));
}

Connection

The connection to the MQTT broker is made when the first message is published and then reused/kept open for later messages.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.