Communicate over MQTT topics

Updated at:

The Java Link SDK provides interfaces for communicating with Alibaba Cloud IoT Platform over custom MQTT topics, including publishing messages, subscribing to topics, and unsubscribing from topics.

For the complete code, see MqttSample.java in the demo.

Publish messages

For more information, see MqttPublishRequest.

  • Without an acknowledgement

    Use this method when a device sends a message and does not require an acknowledgement from IoT Platform, or when IoT Platform sends a reply that the device does not need to process.

    // Publish the message.
    MqttPublishRequest request = new MqttPublishRequest();
    // Specify whether to require an acknowledgement.
    request.isRPC = false;
    // Set the topic. The device uses this topic to send messages to IoT Platform. The following topic is an example. Replace it with your device's topic.
    request.topic = "/a18wP******/LightSwitch/user/update";
    // Set the QoS level.
    request.qos = 0;
    String data = "hello world"; // TODO: Set the data to publish.
    request.payloadObj = data;
    LinkKit.getInstance().publish(request, new IConnectSendListener() {
        @Override
        public void onResponse(ARequest aRequest, AResponse aResponse) {
            // The message is successfully submitted to the operating system's send buffer.
            // The message may fail to reach the cloud due to network fluctuations or other issues.
            // If the mobile originated message has a corresponding mobile terminated reply, use the reply message to confirm that the mobile originated message was received.
        }
        @Override
        public void onFailure(ARequest aRequest, AError aError) {
            // The message failed to be published.
        }
    });                       

    Parameter

    Example

    Description

    isRPC

    false

    Specifies whether the request is an RPC request. If it is, the system waits for a message from the replyTopic before sending a response.

    The default value is false, which means no acknowledgement is required.

    topic

    /a18wP******/LightSwitch/user/update

    A topic to which the device has publish permissions, used to send messages to IoT Platform.

    qos

    0

    The Quality of Service (QoS) level for the MQTT request. The default value is 0.

    payloadObj

    {"id":"160865432","method":"thing.event.property.post","params":{"LightSwitch":1},"version":"1.0"}

    The data to publish. The data can be in any format. If the format is a JSON string, the value of the id field must be unique for each message. Use auto-increment to set the ID. For example, if the id is 160865432, the next id must be 160865433.

  • Return acknowledgement

    • Use this method when messages from a device are forwarded to your server, which then sends reply messages back to the device.

    • Also applies when you use an Alink protocol topic that requires a server-side reply.

    // Publish the message.
    MqttPublishRequest request = new MqttPublishRequest();
    // Specify whether to require an acknowledgement. Set to true to expect a mobile terminated reply from IoT Platform.
    request.isRPC = true;
    // Set the QoS level.
    request.qos = 0;
    // Set the topic. The device uses this topic to send messages to IoT Platform. The following topic is an example. Replace it with your device's topic.
    request.topic = "/a18wP******/LightSwitch/user/update";
    // Set the topic for replies from IoT Platform. If not set, the default is topic + "_reply".
    request.replyTopic = "/a18wP******/LightSwitch/user/update_reply";
    String data = "hello world"; // TODO: Set the data to send.
    request.payloadObj = data;
    LinkKit.getInstance().publish(request, new IConnectSendListener() {
        @Override
        public void onResponse(ARequest aRequest, AResponse aResponse) {
            // The message was published successfully.
        }
        @Override
        public void onFailure(ARequest aRequest, AError aError) {
            // The message failed to be published.
        }  
    });                    

    Parameter

    Example

    Description

    isRPC

    true

    Specifies whether the request is an RPC request. If it is, the system waits for a message from the replyTopic before sending a response.

    Set this to true to expect a mobile terminated reply from IoT Platform.

    qos

    0

    The Quality of Service (QoS) level for the MQTT request. The default value is 0.

    topic

    /a18wP******/LightSwitch/user/update

    A topic to which the device has publish permissions, used to send messages to IoT Platform.

    replyTopic

    /a18wP******/LightSwitch/user/update_reply

    The topic for replies from IoT Platform. If not set, the default is topic + "_reply".

    payloadObj

    {"id":"160865432","method":"thing.event.property.post","params":{"LightSwitch":1},"version":"1.0"}

    The data to publish. The data must be in a JSON string format. The value of the id field must be unique for each message. Use auto-increment to set the ID. For example, if the id is 160865432, the next id must be 160865433.

Subscribe to messages

Subscribe to a topic to receive its messages. IoT Platform saves the subscription and forwards messages published to that topic to the device. For more information, see MqttSubscribeRequest.

After you subscribe to a topic, mobile terminated messages are passed to the `IConnectNotifyListener` object. For more information, see Connection status and mobile terminated message listener in Authentication and connection.

Sample code:

// Subscribe to a topic.
MqttSubscribeRequest subscribeRequest = new MqttSubscribeRequest();
// Replace subTopic with the topic to which you want to subscribe.
subscribeRequest.topic = subTopic;
subscribeRequest.isSubscribe = true;
LinkKit.getInstance().subscribe(subscribeRequest, new IConnectSubscribeListener() {
    @Override
    public void onSuccess() {
        // The subscription is successful.
    }
    @Override
    public void onFailure(AError aError) {
        // The subscription failed.
    }
});                    

If a device no longer needs messages from a topic, unsubscribe from it. Otherwise, the subscription persists and the device continues to receive messages. For more information, see Unsubscribe from a topic below.

Unsubscribe

// Unsubscribe
MqttSubscribeRequest unsubRequest = new MqttSubscribeRequest();
// Replace unSubTopic with the topic from which you want to unsubscribe.
unsubRequest.topic = unSubTopic;
unsubRequest.isSubscribe = false;
LinkKit.getInstance().unsubscribe(unsubRequest, new IConnectUnscribeListener() {
    @Override
    public void onSuccess() {
        // The unsubscription is successful.
    }
    @Override
    public void onFailure(AError aError) {
        // The unsubscription failed.
    }
});