Message Queue for MQTT provides a fundamental feature that allows you to send and receive messages. Clients and backend service applications can be connected to the Message Queue for MQTT broker. This allows you to implement messaging between clients or between clients and backend service applications.Message Queue for MQTT This topic describes the basic scenarios of this feature and provides demos that you can use to send and receive messages.

Background information

In a message interaction scenario in Message Queue for MQTT, both clients and backend service applications can serve as message producers or consumers. Clients must use the client SDK and backend service applications must use the cloud SDK of Message Queue for MQTT to be connected to Message Queue for MQTT to send and receive messages.

Message Queue for MQTT provides the following SDKs:
  • Client SDK: an open source SDK of Message Queue Telemetry Transport (MQTT), which supports the MQTT 3.1.1 protocol and is applicable to client-related development.
  • Cloud SDK: an SDK developed by Alibaba Cloud, which is applicable to the development of backend service applications. The cloud SDK can be used to send and receive messages, and receive status notifications from the client SDK.

Message interaction between clients

Message interaction between clients

In this scenario, messages are sent and received in the mobile environment. The clients are connected to Message Queue for MQTT based on the MQTT protocol. They can use the open source client SDK to be connected to Message Queue for MQTT to send and receive messages.

Examples:

  • Instant messaging (IM): For example, two mobile phones on which a chat app is installed are connected to the Message Queue for MQTT broker to send and receive messages.
  • Smart device management: For example, an app on a smart phone instructs a power bank that is connected to the Message Queue for MQTT broker to be ejected. The power bank is automatically ejected after it receives the instruction.

Message interaction between clients and backend service applications

In this scenario, the message producer and consumer are distributed in the mobile environment and the cloud. Clients and backend service applications communicate with each other by using Message Queue for MQTT. Clients use the client SDK to be connected to the Message Queue for MQTT broker, whereas backend service applications use the cloud SDK to be connected to the Message Queue for MQTT broker.

Examples:

  • Device status report: In this case, clients send messages, whereas backend service applications receive messages. For example, a large number of digital price tags in the mobile environment regularly report their display statuses and power consumption. The backend service applications in the cloud analyze the statuses of the digital price tags based on the reported data and make adjustment based on business requirements.
  • System message push: In this case, backend service applications send messages, whereas clients receive messages. For example, a game application in the cloud sends an announcement indicating that the game service will be suspended for updates. The Message Queue for MQTT broker pushes the announcement to all the installed mobile clients of the game application to notify game users of the announcement in the form of notifications.
  • Message receiving: The cloud SDK supports the clustering consumption mode. In other words, consumers in the same group can subscribe to and consume different messages.

Demos for you to use the client SDK

Note The following demos provide examples on how to use SDKs to send and receive messages. The programming language that is used in a demo provided for a scenario is not the only supported programming language in the scenario. You can modify parameter settings for different programming languages based on the demo in Java.

Send and receive messages

Programming language URL
Java mqtt.demo.java
C mqtt.demo.c
Python mqtt.demo.python
.NET mqtt.demo.net
JavaScript mqtt.demo.javascript
iOS mqtt.demo.ios
PHP mqtt.demo.php
Programming language URL
JavaScript mqtt.demo.javascript

Client authentication

When a client is connected to the Message Queue for MQTT broker to send and receive messages, you can use one of the following methods to authenticate the client. For more information, see Authentication overview.

Note If you use the signature authentication mode to send and receive messages, you need to set the Username and Password parameters. For more information about how to obtain the values of the parameters to verify signatures, see Signature authentication.

Programming language URL
Java mqtt.token.mode.demo.java
PHP mqtt.token.mode.demo.php

Demos for you to use the cloud SDK

Note Only Java is supported for the cloud SDK.
Programming language URL
Java MQTTConsumerDemo.java MQTTProducerDemo.java