All Products
Search
Document Center

Function Compute:ApsaraMQ for MQTT triggers

Last Updated:Jul 25, 2023

After ApsaraMQ for MQTT is integrated with Function Compute by using EventBridge, functions can be triggered by using an ApsaraMQ for MQTT trigger. The functions can process messages that are published to ApsaraMQ for MQTT. This topic describes how to create an ApsaraMQ for MQTT trigger, configure input parameters, and write and test code in the Function Compute console.

Background information

After you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates an event stream resource in EventBridge based on the trigger configurations.

After the trigger is created, you can view information about the trigger in the Function Compute console. You can also view information about the created resources in the EventBridge console. When a message is enqueued in ApsaraMQ for MQTT, the execution of a function in Function Compute is triggered. One or more message events are pushed to the function in batches based on your batch configurations.

Before you start

Limits

  • The ApsaraMQ for MQTT instance that is used as the trigger source must be in the same region as the function in Function Compute.

  • If the number of event streams exceeds the upper limit, the ApsaraMQ for MQTT triggers can no longer be created. For the upper limit of event streams, see Limits.

Step 1: Create an ApsaraMQ for MQTT trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click the name of the desired function.
  4. On the function details page, click the Triggers tab, select a version or alias from the Version or Alias drop-down list, and then click Create Trigger.

  5. In the Create Trigger panel, configure the parameters and click OK.

    Parameter

    Description

    Example

    Trigger Type

    Select ApsaraMQ for MQTT.

    ApsaraMQ for MQTT

    Name

    Enter a trigger name.

    mqtt-trigger

    Version or Alias

    The default value is LATEST. If you want to create a trigger for another version or alias, switch to the trigger of the version or alias in the upper-right corner of the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.

    LATEST

    ApsaraMQ for MQTT Instance

    Select an ApsaraMQ for MQTT instance.

    mqtt-xxx

    MQTT Topic

    Select an ApsaraMQ for MQTT topic.

    testTopic

    Invocation Method

    Select the method to invoke the function.

    Options:

    • Sync Invocation: After an event triggers the function, Function Compute waits for the function invocation to complete before it returns responses. For more information, see Synchronous invocations.

    • Async Invocation: After an event triggers the execution of a function, Function Compute immediately returns a response and ensures that the function is successfully executed at least once. However, the detailed execution result is not returned. This invocation method is suitable for functions that have higher scheduling latency. For more information, see Overview.

    Sync Invocation

    Trigger State

    Specify whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected and the trigger is immediately enabled after it is created.

    Enable Trigger

    For more information about advanced configurations such as push settings, retry policies, and dead letter queues, see Advanced features of triggers.

    The trigger is displayed in the Triggers tab after it is created. To modify or delete a created trigger, see Manage triggers.

Step 2: Configure the input parameters of the function

An ApsaraMQ for MQTT event source is passed to the function in the form of event, which acts as the input parameter. You can manually pass event to the function to trigger the function.

  1. On the function details page, click the Code tab and click the xialatubiao icon. From the drop-down list that appears, select Configure Test Parameters.
  2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. Then, click OK.

    Sample code of event:

    [
      {
        "props": {
          "firstTopic": "testTopic",
          "secondTopic": "/testMq4****",
          "clientId": "consumerGroupID@@@xxx"
        },
        "body": "hello mq4Iot pub sub msg"
      }
    ]

    The following table describes parameters that are contained in event.

    Parameter

    Type

    Example

    Description

    props

    Map

    The properties of the message.

    firstTopic

    String

    testTopic

    The parent topic that is used to send and receive messages.

    secondTopic

    String

    /testMq4****

    The subtopic.

    clientId

    String

    consumerGroupID@@@xx

    The ID of the client.

    body

    String

    hello mq4Iot pub sub msg

    The content of the message.

Step 3: Write and test the function

After you create the trigger, you can write function code and test the function to verify whether the code is correct. When ApsaraMQ for MQTT receives a message, the trigger automatically triggers the function.

  1. On the function details page, click the Code tab, enter function code in the code editor, and then click Deploy.

    In this topic, sample code in a Node.js runtime is used as an example.

    'use strict';
    /*
    To enable the initializer feature
    please implement the initializer function as below:
    exports.initializer = (context, callback) => {
      console.log('initializing');
      callback(null, '');
    };
    */
    exports.handler = (event, context, callback) => {
      console.log("event: %s", event);
      // Parse the event parameters and process the event. 
      callback(null, 'return result');
    }
  2. Click the Code tab and click Test Function.
    After the function is executed, you can view the result on the Code tab.

Additional information

In addition to the Function Compute console, you can configure triggers by using the following methods:

  • Use Serverless Devs to configure triggers. For more information, see Create a trigger.

  • Use SDKs to configure triggers. For more information, see SDKs.

To modify or delete an existing trigger, see Manage triggers.