After Alibaba Cloud Message Service (MNS) is integrated with Alibaba Cloud Function Compute, MNS topics are used as event sources to connect to Function Compute. That is, functions are subscribers to topics. An MNS topic trigger is used to trigger a function to perform custom processing on messages that are published to a topic.

Background

MNS is an efficient, reliable, secure, convenient, and scalable distributed message service. MNS helps application developers freely transfer data and messages between distributed components of their applications to build loosely coupled systems. In MNS, a topic is the destination to which messages are published. A publisher can call the PublishMessage operation to publish a message to a topic and a subscriber to the topic receives the message. For more information about the PublishMessage operation, see PublishMessage. For more information about MNS, see MNS documentation.

Configuring a trigger for an MNS topic is equivalent to registering a function as a subscriber to this MNS topic. When a publisher publishes a message to this MNS topic, the message is passed to the function and used as the event parameter of the function handler to trigger the function. For more information, see Input parameters of functions.

Currently, MNS supports specified subscribers, but is not flexibly enough. The integration of MNS and Function Compute has the following benefits:
  • Allows you to perform advanced processing on a message before you send this message in an email or an SMS message.
  • Allows you not to create any services at HTTP endpoints.
  • Supports abundant custom processing. For example, you can send a message to Slack or persist a specified message.

Notes

  • We strongly recommend that you deploy MNS topics and functions of Function Compute in the same region.

    An MNS topic trigger allows a function to subscribe to a topic that is located in a different region. However, network latency is increased when the function and topic are located in different regions, especially when the function is located in China and the topic is located outside China, or vice versa.

  • You must avoid recursive calls.

    Avoid the following logic when you write a function: Topic A triggers function B and function B publishes a new message to topic A. This logic results in an infinite loop of function calls.