All Products
Search
Document Center

ApsaraMQ for MQTT:Demo projects

Last Updated:Sep 07, 2023

This topic describes demo projects for ApsaraMQ for MQTT and provides usage notes for ApsaraMQ 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

mqtt.demo.java

Example on how to use the signature authentication mode of ApsaraMQ for MQTT

mqtt.signature.mode.demo.java

Example on how to use the token-based authentication mode of ApsaraMQ for MQTT

mqtt.token.mode.demo.java

Example on how to use the SSL encryption feature of ApsaraMQ for MQTT

mqtt.ssl.demo.java

Example on how to use ApsaraMQ for MQTT to send messages and ApsaraMQ for RocketMQ to subscribe to messages

mqtt.to.rocketmq.demo.java

Example on how to use ApsaraMQ for RocketMQ to send messages and ApsaraMQ for MQTT to subscribe to messages

rocketmq.to.mqtt.demo.java

Example on how to use ApsaraMQ for MQTT to send ordered messages and ApsaraMQ for RocketMQ to subscribe to ordered messages

mqtt.ordered.msg.demo.java

Example on how to configure asynchronous status notifications for ApsaraMQ for MQTT clients

mqtt.client.status.notice.demo.java

Example on how to use the SSL mutual authentication mode of ApsaraMQ for MQTT

mqtt.bissl.demo.java

Python

Scenario

Download link

Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages

mqtt.demo.python

Example on how to use the signature authentication mode of ApsaraMQ for MQTT

mqtt.signature.mode.demo.python

Example on how to use the SSL encryption feature of ApsaraMQ for MQTT

mqtt.ssl.mode.demo.python

PHP

Scenario

Download link

Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages

mqtt.demo.php

Example on how to use the signature authentication mode of ApsaraMQ for MQTT

mqtt.signature.mode.demo.php

Example on how to use the token-based authentication mode of ApsaraMQ for MQTT

mqtt.token.mode.demo.php

Example on how to use ApsaraMQ for MQTT to send ordered messages and ApsaraMQ for RocketMQ to subscribe to ordered messages

mqtt.ordered.msg.demo.php

C

Scenario

Download link

Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages

mqtt.demo.c

Example on how to use the signature authentication mode of ApsaraMQ for MQTT

mqtt.signature.mode.demo.c

Example on how to use the SSL encryption feature of ApsaraMQ for MQTT

mqtt.ssl.demo.c

Note

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

mqtt.demo.net

Example on how to use the signature authentication mode of ApsaraMQ for MQTT

mqtt.signature.mode.demo.net

Example on how to use the SSL encryption feature of ApsaraMQ for MQTT

mqtt.ssl.mode.demo.net

JavaScript

Scenario

Download link

Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages

mqtt.demo.javascript

Example on how to use the signature authentication mode of ApsaraMQ for MQTT

mqtt.signature.mode.demo.javascript

Example on how to use the SSL encryption feature of ApsaraMQ for MQTT

mqtt.ssl.demo.javascript

Example on how to use WebSocket to send and subscribe to messages in ApsaraMQ for MQTT

mqtt.websocket.demo.javascript

iOS

Scenario

Download link

Example on how to use only ApsaraMQ for MQTT to send and subscribe to messages

mqtt.demo.ios

Example on how to use the signature authentication mode of ApsaraMQ for MQTT

mqtt.signature.mode.demo.ios

Example on how to use the SSL encryption feature of ApsaraMQ for MQTT

mqtt.ssl.demo.ios

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 ApsaraMQ for MQTT and ApsaraMQ for RocketMQ.

Note

The integrity of a demo varies based on the programming language. Updates for demos will be provided in future versions. If a feature is unavailable in the demo, download the SDK for the corresponding programming language and then debug 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 items describe how to configure the environment variables:

Important

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

    Run the following commands to configure the environment variables:

    export MQTT_AK_ENV=<access_key_id>
    export MQTT_SK_ENV=<access_key_secret>

    Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret.

  • Windows operating system

    1. Create an environment variable file, add the MQTT_AK_ENV and MQTT_SK_ENV environment variables to the file, and then write your AccessKey ID and AccessKey secret to the file.

    2. Restart the Windows operating system.

References

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.