Skip to content

Subscribe via trigger

Kees Schollaart edited this page Apr 14, 2018 · 5 revisions

Trigger Binding

  • Add the MqttTriggerAttribute to one of your functions parameters
  • Use one of the constructor overloads to configure
  • Usually it is sufficient to add one ore more topics to the attribute
  • Make sure the type of the parameter is IMqttMessage

Example

[FunctionName("SimpleFunction")]
public static void SimpleFunction(
        [MqttTrigger("testtopic/in")]IMqttMessage message,
        ILogger logger)
    {
        var body = message.GetMessage();
        var bodyString = Encoding.UTF8.GetString(body);
        logger.LogInformation($"{DateTime.Now:g} Message for topic {message.Topic}: {bodyString}");
    }

Connection

The connection to the MQTT broker is made when the function boots. Internally this is implemented using MQTTNet's Managed Client which manages the connections state.