After you integrate Message Queue for MQTT with Function Compute by using EventBridge as an event source, you can use a Message Queue for MQTT trigger to invoke a function that customizes the processing of messages published to Message Queue for MQTT. This topic describes how to create an MQTT trigger, configure input parameters, and write and test code in the Function Compute console.
Background information
When you create a trigger in the Function Compute console, Function Compute automatically creates an event stream resource in EventBridge based on the trigger configuration.
After the trigger is created, you can view its details in the Function Compute console. You can also view the automatically created resources in the EventBridge console. When a message arrives in Message Queue for MQTT, the trigger invokes the function. Depending on your batching configuration, one or more message events are pushed to the function in a batch for processing.
Prerequisites
EventBridge
Function Compute
Message Queue for MQTT
Limits
The Message Queue for MQTT instance and the Function Compute function must be in the same region.
If you reach the maximum number of event streams, you cannot create more MQTT triggers. For more information about event stream limits, see Limits.
Step 1: Create an MQTT trigger
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Trigger tab and then click Create Trigger.
In the Create Trigger panel, enter the required information and click OK.
Parameter
Actions
Example
Trigger Type
Select ApsaraMQ for MQTT.
Message Queue for MQTT
Name
Enter a custom name for the trigger.
mqtt-trigger
Version or Alias
The default value is LATEST. If you need to create a trigger for a different version or alias, switch to that version or alias in the upper-right corner of the function details page. For more information, see Manage versions and Manage aliases.
LATEST
ApsaraMQ for MQTT Instance
Select an existing MQTT instance.
mqtt-xxx
MQTT topic
Select an existing MQTT topic.
testTopic
Invocation Method
Select the function's invocation method.
Valid values:
Sync Invocation: This is the default method. When an event triggers the function, Function Compute waits for the invocation to complete before returning the result. For more information, see synchronous invocations.
Async Invocation: This method is suitable for functions with long execution times. When an event triggers the function, Function Compute returns a response immediately. The service ensures that the function is executed at least once, but the execution result is not returned in the response. For more information, see asynchronous invocation.
Synchronous Invocation
Trigger State
Specifies whether to enable the trigger immediately after creation. By default, this option is selected.
Enable Trigger
For information about advanced settings such as push configuration, retry policies, and dead-letter queues, see Advanced features of triggers.
After the trigger is created, it appears on the Triggers tab. To modify or delete the trigger, see Manage triggers.
Step 2: Configure function input parameters
An MQTT event source is passed to a function as an input parameter in the form of an event. You can manually pass an event to the function to simulate an event trigger.
On the Code tab of the function details page, click the
icon next Test Function and select Configure Test Parameters from the drop-down list. In the Configure Test Parameters panel, select Create New Test Event or Modify Existing Test Event, enter an event name and the event content, and click OK.
The event is in the following format.
[ { "props": { "firstTopic": "testTopic", "secondTopic": "/testMq4****", "clientId": "consumerGroupID@@@xxx" }, "body": "hello mq4Iot pub sub msg" } ]The following table describes the parameters in the event object.
Parameter
Type
Example
Description
props
Map
The message attributes.
firstTopic
String
testTopic
The parent topic used to send and receive messages.
secondTopic
String
/testMq4****
The child topic.
clientId
String
consumerGroupID@@@xx
The client ID.
body
String
hello mq4Iot pub sub msg
The message body.
Step 3: Write and test the function code
After you create the trigger, you can write and test your function code to verify that it works correctly. In a production environment, the trigger automatically invokes the function when a message is received by Message Queue for MQTT.
On the function details page, go to the Code tab. In the code editor, write your code and click Deploy.
The following sample code is written in Node.js.
'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'); }Click Test Function.
More information
In addition to the Function Compute console, you can configure triggers by using one of the following methods:
Use Serverless Devs tool to configure triggers. For more operations, please refer to Common Serverless Devs commands.
Use SDKs to configure triggers. For more information, see SDKs.
To modify or delete an existing trigger, see Manage triggers.