This topic describes common issues that occur when you connect the ApsaraMQ for MQTT client to the server.
What is the difference between one-way and mutual authentication?
In one-way authentication, the device verifies the server certificate to confirm the broker's identity and then starts encrypted transmission. Mutual authentication adds server-side verification of the device certificate in addition to this. If you only need encrypted transmission, use one-way authentication.
In one-way authentication, Alibaba Cloud provides a server certificate issued by a trusted authority (signed by GlobalSign, the same root certificate authority that aliyun.com uses). This root certificate is typically pre-installed in operating systems, browsers, and programming language runtimes. Therefore, devices can directly verify and trust the MQTT broker certificate.
What should I note when configuring mutual authentication?
Follow the documentation to configure the CA certificate, server certificate, and device certificate in order. For details, see Certificate management.
During configuration, pay attention to the following:
When managing server certificates, you do not need to specify the CN field. Ensure that the alt_names in server.crt.cfg include *.mqtt.aliyuncs.com.
When managing device certificates, do not modify the CN field in the certificate file.
How do I install the MQTT .NET SDK?
The current version requires RabbitMQ Client 6.5.0. Earlier or later versions might cause compatibility issues. For more information, see Version Guide.
You can search for and install the required package at: https://www.nuget.org/packages.
For connection and authentication examples, see Sample code.
How do I fix errors when sending point-to-point messages with C# .NET?
First, download the M2Mqtt package: https://www.nuget.org/packages/M2Mqtt.
For details about point-to-point message sending and receiving, see Point-to-point message sending and receiving (MQTT).
How do I fix the "Too many publishes in progress" error?
Cause: The publish TPS per device is too high.
Solution: Limit the publish TPS per device. You can try increasing the value of mqttConnectOptions.setMaxInflight(). This parameter sets the maximum number of concurrent messages that a single client can send.
Why is there a delay before the first message is received after the client connects?
After the connectComplete callback is triggered, the first message may be delayed by about two seconds. This delay occurs because the client must complete initialization and subscription setup after connecting. This behavior is normal.
How do I use an MQTT client library in PHP?
We recommend using the php-mqtt/client library.
You can run composer install in the directory that contains src. Then run the sample programs using php src/MqttConsumer.php and php src/MqttProducer.php.
How do I fix errors when using an MQTT client library on Android?
Version 1.1.1 of org.eclipse.paho:org.eclipse.paho.client.mqttv3 does not support Android SDK 33 or later. Use the community-maintained hannesa2/paho.mqtt.android library instead. It supports Android SDK 33.
For details, see paho.mqtt.android releases.
Note: ApsaraMQ for MQTT is compatible with the open-source MQTT protocol. However, third-party SDKs might have their own compatibility issues. We recommend using the libraries listed above for development.
How do I connect to a VPC endpoint?
An ECS instance in the same region can connect to a VPC endpoint over the private network. You do not need to use the same Alibaba Cloud account as your ApsaraMQ for MQTT instance.
What is the difference between endpoints for cloud SDKs and device SDKs?
Device SDK endpoint format:
Public endpoint:
{MQTT instance ID}.mqtt.aliyuncs.comVPC endpoint:
{MQTT instance ID}-internal-vpc.mqtt.aliyuncs.com
Cloud SDK endpoint format:
Public endpoint:
{MQTT instance ID}-server-internet.mqtt.aliyuncs.comVPC endpoint:
{MQTT instance ID}-server-internal.mqtt.aliyuncs.com
How does MQTT heartbeat detection work?
After the client sends a heartbeat packet, the broker starts a timer. If the broker does not receive a new heartbeat packet within approximately 1.5 times the heartbeat interval, it closes the connection. If the broker receives a heartbeat packet during this period, it resets the timer.
When can I use a proxy domain name to access MQTT services?
You can use a proxy domain name in the following scenarios:
One-way authentication without TLS: You can access the service directly using the proxy domain name.
Mutual authentication: You can establish a TLS connection using the proxy domain name. Note that the proxy domain name must complete ICP filing to ensure compliance and connectivity.
About IPv6: MQTT services do not support direct IPv6 connections. To access MQTT services from an IPv6 environment, forward IPv6 traffic through a proxy server to an IPv4 domain name. The IPv4 domain name then resolves to the MQTT service endpoint.
Android Demo Reports 'Connection Failed' Error
Problem description
The resources are correctly configured, and the network can be accessed.
The AccessKey ID and AccessKey secret are authorized and correctly entered.
Possible cause
The format of the endpoint is incorrect. Specify the endpoint in one of the following formats: tcp://Endpoint:1883 or tls://Endpoint:8883.
Why can the client connect, but the consumer disconnects immediately upon subscription?
Possible cause: The topic settings or naming format is incorrect. You must check the configuration of your parent topic and subtopic.
Parent topic
MQTT is a messaging protocol based on the publish-subscribe model. Each message belongs to a topic. MQTT supports multi-level topics. A level-1 topic is considered a parent topic. Before you use ApsaraMQ for MQTT, you must create a parent topic in the console. You can create it in the ApsaraMQ for MQTT console or the ApsaraMQ for RocketMQ console.
Subtopic
A level-2 or level-3 topic is a subtopic of a parent topic. You do not need to create subtopics in the console. You can specify them directly in your code. You must name subtopics using forward slashes (/) to separate the parent topic and each subtopic: <Parent topic name>/<Level-2 topic name>/<Level-3 topic name>. For example: SendMessage/demo/producer. In ApsaraMQ for MQTT, the combined length of the parent topic and subtopics cannot exceed 64 characters. Exceeding this limit will cause a client exception.
Why is the number of subscriptions that are displayed in the console higher than the number of subscriptions that are used in actual scenarios?
If the cleanSession parameter is set to false, the subscriptions are not cleared even after the client is disconnected.
If you do not want to retain subscriptions after the client goes offline, use the subscription cleanup feature to automatically clear offline client subscriptions.
Client Keeps Disconnecting and Reconnecting, and Returns "invalid param" Error.
Possible cause: The broker denied the connection request because the size of the message body exceeded the limit. In ApsaraMQ for MQTT, the size of a message body cannot exceed 64 KB.
Business side unchanged, client and server connection suddenly disconnects
Possible causes:
A device shutdown or loss of network connectivity disconnects the client connection.
If you use token-based authentication, the client disconnects after the token expires. If the client fails to obtain a new token before retrying the connection, the connection fails.
How do I specify the heartbeat timeout period of the client?
You can call the MqttConnectOptions class.
Before you use the sample code, configure environment variables for your access credentials. For more information about how to configure environment variables, see Configure access credentials.
The environment variables for the AccessKey ID and AccessKey secret of ApsaraMQ for MQTT are MQTT_AK_ENV and MQTT_SK_ENV.
Client encounters 'valid owner failed' error when connecting
You must check the following items:
Is the instance name specified correctly?
Did you create the group and topic in the console and specify their names correctly?
Did you specify the group or topic correctly for the selected instance?
The device occasionally encounters a "connection reset by peer" error
When Client ID (ClientID) values are duplicated, the clients disconnect each other, causing the server to close the connection. If a client continues to send data over the TCP connection, it triggers the peer socket to send an RST packet.
You must ensure that the client ID you specify is globally unique. For more information, see Client limits.
How do I configure the automatic connection feature for the SDK for Java client in Message Queue for MQTT?
Enable the automatic client reconnection feature for the SDK for Java client
mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);After you set the autoReconnect parameter to true, the Java SDK client in ApsaraMQ for MQTT automatically reconnects.
mqttClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
/**
* The topic that you must subscribe to after the client connection is established.
*/
System.out.println("connect success");
}
@Override
public void connectionLost(Throwable throwable) {
throwable.printStackTrace();
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
/**
* The callback that is called to consume messages. Make sure that the callback does not throw exceptions. If the callback returns, the message is consumed.
* Messages must be consumed within the specified time. If consumption takes longer than the timeout period set by the broker, the broker may resend the message in reliable transmission mode. Make sure that your business logic handles duplicates.
* For information about the timeout period, see Limits.
*/
System.out.println(
"receive msg from topic " + s + " , body is " + new String(mqttMessage.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
System.out.println("send msg succeed topic is : " + iMqttDeliveryToken.getTopics()[0]);
}
});When the connectComplete method in the client callback is invoked, the connection or reconnection becomes successful. You do not need to monitor the connection status. The re-established connection is different from the previous one. Therefore, actions such as sending or subscribing to messages on the previous connection no longer take effect.
At this point, you must resubscribe to the topic so that the business side can continue to send subscription messages over the new internal connection.
@Override
public void connectComplete(boolean reconnect, String serverURI) {
/**
* The topic that you must subscribe to after the client connection is established.
*/
System.out.println("connect success");
executorService.submit(new Runnable() {
@Override
public void run() {
try {
final String topicFilter[] = {mq4IotTopic};
final int[] qos = {qosLevel};
mqttClient.subscribe(topicFilter, qos);
} catch (MqttException e) {
e.printStackTrace();
}
}
});
}Manual client connection maintenance on the application side
mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(false);If the autoReconnect parameter is set to false, the ApsaraMQ for MQTT client does not automatically reconnect after it is disconnected.
When the connection is lost, the business side detects an abnormal condition. You must manually shut down the legacy connection and then establish a new one.