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
Python
PHP
C
.NET
JavaScript
iOS
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 |
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 |
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
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
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.
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
Use CMD
Use Windows PowerShell
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 | |
AccessKey Secret | |
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.
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 LTAI**** /M
setx MQTT_SK_ENV EZJn**** /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.
In PowerShell, configure new environment variables. The environment variables apply to all new sessions.
[System.Environment]::SetEnvironmentVariable('MQTT_AK_ENV', 'LTAI****', [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable('MQTT_SK_ENV', 'EZJn****', [System.EnvironmentVariableTarget]::User)
Configure environment variables for all users. You must run the following commands as an administrator.
[System.Environment]::SetEnvironmentVariable('MQTT_AK_ENV', 'LTAI****', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('MQTT_SK_ENV', 'EZJn****', [System.EnvironmentVariableTarget]::Machine)
Configure temporary environment variables. The environment variables apply only to the current session.
$env:MQTT_AK_ENV = "LTAI****"
$env:MQTT_SK_ENV = "EZJn****"
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.