All Products
Search
Document Center

ApsaraMQ for MQTT:Demo projects

Last Updated:Aug 07, 2024

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

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

Note

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.

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

  1. Create the .bash_profile file.

    Note

    If you configure environment variables for the first time, you can run the touch ~/.bash_profile command to create the configuration file. If the configuration file already exists, run the vim ~/.bash_profile command to edit the configuration file.

    touch ~/.bash_profile
    vim ~/.bash_profile
  2. Configure the .bash_profile file.

    # Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> in the following commands with your AccessKey ID and AccessKey secret. 
    export MQTT_AK_ENV=<ACCESS_KEY_ID>
    export MQTT_SK_ENV=<ACCESS_KEY_SECRET>

    Save the file after the configuration.

  3. Use the .bash_profile file.

    # Run the following command in the configuration file.
    source ~/.bash_profile
    # Check whether the configuration takes effect.
    echo $MQTT_AK_ENV

    If the returned AccessKey pair is valid, the configuration is successful.

Important

After you configure the environment variables, restart your development tools or services, such as the integrated development environment (IDE). Otherwise, the new settings may not take effect as expected.

Windows operating system

Use GUI

  • Procedure

    If you want to use GUI to configure environment variables in Windows 10, perform the following steps:

    On the Windows desktop, right-click This PC and select Properties. On the page that appears, click Advanced system settings. In the System Properties dialog box, click Environment Variables on the Advanced tab. In the Environment Variables dialog box, click New in the User variables or System variables section. Then, configure the variables described in the following table.

    Variable

    Example

    AccessKey ID

    • Variable name: MQTT_AK_ENV

    • Variable value: LTAI4GDty8ab9W4Y1D****

    AccessKey Secret

    • Variable name: MQTT_SK_ENV

    • Variable value: IrVTNZNy5yQelTETg0cZML3TQn****

  • Check whether the configuration is successful.

    Click Start or press Win+R. In the Run dialog box, enter cmd and click OK or press the Enter key to open the Command Prompt. Then, run the echo %MQTT_AK_ENV% and echo %MQTT_SK_ENV% commands. If the valid AccessKey pair is returned, the configuration is successful.

Use CMD

  • Procedure

    Open a Command Prompt window as an administrator and run the following commands to add environment variables in the operating system:

    setx MQTT_AK_ENV LTAI4GDty8ab9W4Y1D**** /M
    setx MQTT_SK_ENV IrVTNZNy5yQelTETg0cZML3TQn**** /M

    /M indicates that the environment variable is of system level. You cannot use this parameter when you configure a user-level environment variable.

  • Check whether the configuration is successful.

    Click Start or press Win+R. In the Run dialog box, enter cmd and click OK or press the Enter key to open the Command Prompt. Then, run the echo %MQTT_AK_ENV% and echo %MQTT_SK_ENV% commands. If the valid AccessKey pair is returned, the configuration is successful.

Use Windows PowerShell

In PowerShell, configure new environment variables. The environment variables apply to all new sessions.

[System.Environment]::SetEnvironmentVariable('MQTT_AK_ENV', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable('MQTT_SK_ENV', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::User)

Configure environment variables for all users. You must run the following commands as an administrator.

[System.Environment]::SetEnvironmentVariable('MQTT_AK_ENV', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('MQTT_SK_ENV', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::Machine)

Configure temporary environment variables. The environment variables apply only to the current session.

$env:MQTT_AK_ENV = "LTAI4GDty8ab9W4Y1D****"
$env:MQTT_SK_ENV = "IrVTNZNy5yQelTETg0cZML3TQn****"

In PowerShell, run the Get-ChildItem env:MQTT_AK_ENV and Get-ChildItem env:MQTT_SK_ENV commands. If the returned AccessKey pair is valid, the configuration is successful.

Important

After you configure the environment variables, restart your development tools or services, such as the integrated development environment (IDE). Otherwise, the new settings may not take effect as expected.

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.