This topic describes the demo projects for ApsaraMQ for MQTT and provides usage notes for using Message Queue for MQTT in various scenarios. For more information, see the comments of the source code of the demos.
Obtain demos for multiple programming languages
For the complete set of sample code, see mqtt-demo.
After you decompress the downloaded package, a folder named mqtt-demo-main appears on your on-premises machine. Level-1 folders are classified based on programming languages. Perform debugging based on your business requirements.
Java
Scenario | Download link |
Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages | |
Example on how to use the signature authentication mode of ApsaraMQ for MQTT | |
Example on how to use the token-based authentication mode of ApsaraMQ for MQTT | |
Example on how to use the SSL encryption feature of ApsaraMQ for MQTT | |
Example on how to use ApsaraMQ for MQTT to send messages and ApsaraMQ for RocketMQ to subscribe to messages | |
Example on how to use ApsaraMQ for RocketMQ to send messages and ApsaraMQ for MQTT to subscribe to messages | |
Example on how to use ApsaraMQ for MQTT to send ordered messages and ApsaraMQ for RocketMQ to subscribe to ordered messages | |
Example on how to configure asynchronous status notifications for ApsaraMQ for MQTT clients | |
Example on how to use the SSL mutual authentication mode of ApsaraMQ for MQTT |
Python
Scenario | Download link |
Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages | |
Example on how to use the signature authentication mode of ApsaraMQ for MQTT | |
Example on how to use the SSL encryption feature of ApsaraMQ for MQTT |
PHP
Scenario | Download link |
Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages | |
Example on how to use the signature authentication mode of ApsaraMQ for MQTT | |
Example on how to use the token-based authentication mode of ApsaraMQ for MQTT | |
Example on how to use ApsaraMQ for MQTT to send ordered messages and ApsaraMQ for RocketMQ to subscribe to ordered messages |
C
Scenario | Download link |
Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages | |
Example on how to use the signature authentication mode of ApsaraMQ for MQTT | |
Example on how to use the SSL encryption feature of ApsaraMQ for MQTT |
For information about dependency libraries for C, see eclipse/paho.mqtt.c.
.NET
Scenario | Download link |
Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages | |
Example on how to use the signature authentication mode of ApsaraMQ for MQTT | |
Example on how to use the SSL encryption feature of ApsaraMQ for MQTT |
JavaScript
Scenario | Download link |
Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages | |
Example on how to use the signature authentication mode of ApsaraMQ for MQTT | |
Example on how to use the SSL encryption feature of ApsaraMQ for MQTT | |
Example on how to use WebSocket to send and subscribe to messages in ApsaraMQ for MQTT |
iOS
Scenario | Download link |
Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages | |
Example on how to use the signature authentication mode of ApsaraMQ for MQTT | |
Example on how to use the SSL encryption feature of ApsaraMQ for MQTT |
Usage notes
If you use ApsaraMQ for MQTT together with other backend message storage services such as ApsaraMQ for RocketMQ, make sure that you understand the mappings between message structures and attributes. For more information, see Message structure mappings between MQTT and RocketMQ.
The integrity of a demo varies based on the programming language. Updates for demos will be provided in future versions. If no demo is available for a feature, download the SDK for the corresponding programming language to test the feature. For more information, see Java examples.
Configure an access credential
Before you use sample code, you must configure the MQTT_AK_ENV and MQTT_SK_ENV environment variables. The following section describes how to configure the environment variables.
The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. If the AccessKey pair of an Alibaba Cloud account is leaked, all resources that belong to the account are exposed to potential risks. To ensure account security, we recommend that you use the AccessKey pair of a RAM user to access API operations and perform routine O&M. For information about how to obtain an AccessKey pair, see Create an AccessKey pair.
Linux and macOS operating systems
Windows operating system
Additional information
The sample code is provided only for reference. For information about how to obtain the SDK for a specific programming language, see Download the SDK.