ApsaraMQ for MQTT provides the messaging feature that allows you to send and receive messages. You can connect devices or cloud services to ApsaraMQ for MQTT brokers. This way, you can use ApsaraMQ for MQTT to send and receive messages between devices or between a device and the cloud. This topic describes the messaging scenarios and provides sample code to show how to send and receive messages.

Background

In a messaging scenario in which ApsaraMQ for MQTT is used, both devices and cloud services can be used as message producers or consumers. You must use an SDK of ApsaraMQ for MQTT to connect to a ApsaraMQ for MQTT broker to send and receive messages, regardless of whether you are using a device or a cloud service.

ApsaraMQ for MQTT provides the following SDKs:
  • Client SDKs: the open source SDKs of Message Queue Telemetry Transport (MQTT). The client SDKs support the MQTT 3.1.1 protocol and are suitable for use by devices.
  • Cloud SDKs: the SDKs that are developed by Alibaba Cloud. The cloud SDKs are suitable for use by cloud services. The cloud SDKs can be used to send and subscribe to messages and receive status notifications that are sent from the client SDKs.

Messaging between devices

Messaging between devices

In this scenario, message producers and consumers run on mobile devices that are connected to ApsaraMQ for MQTT over the MQTT protocol. The devices are connected to ApsaraMQ for MQTT by using the client SDKs.

Typical scenarios

  • Instant messaging: For example, two mobile phones on which a chat app is installed are connected to a ApsaraMQ for MQTT broker to send and receive messages between each other.
  • Smart device management: For example, an app on a mobile phone sends an ejection command to a shared power bank that is connected to a ApsaraMQ for MQTT broker. The shared power bank receives the command and is automatically ejected.

Messaging between devices and the cloud

Messaging between devices and the cloud

In this scenario, message producers are deployed on mobile devices, and message consumers are deployed on the Alibaba Cloud. The devices and the cloud communicate with each other by using ApsaraMQ for MQTT. The devices use client SDKs to connect to ApsaraMQ for MQTT brokers, and the cloud applications use cloud SDKs to connect to ApsaraMQ for MQTT brokers.

Typical scenarios

  • Device status reporting: In this scenario, messages are sent by devices and consumed by cloud applications. For example, a large number of digital price tags that are deployed on devices regularly report their display statuses and power consumption to the management application in the cloud. The cloud application analyzes the statuses of the digital price tags based on the reported data and adjusts resources accordingly.
  • System message pushing: In this scenario, messages are sent by cloud applications and consumed by devices. For example, a game application in the cloud sends an announcement to notify players that services will be suspended for updates. The ApsaraMQ for MQTT broker pushes the announcement to players by sending a message to all the mobile phones on which the game is installed.
  • Message subscription: The cloud SDKs support the cluster consumption mode in which different groups of consumer clients can subscribe to different messages from the cloud application.

Sample code for client SDKs

Note The following sample code provides examples on how to use a client SDK to send and receive messages. The Java programming language that is used in the sample code is not the only supported programming language in the scenario. You can modify parameter settings for different programming languages by referring to the sample code for Java.

Send and receive messages

Programming language Download link
Java mqtt.demo.java
C mqtt.demo.c
Python mqtt.demo.python
.NET mqtt.demo.net
JavaScript mqtt.demo.javascript
iOS mqtt.demo.ios
PHP mqtt.demo.php
Programming language Download link
JavaScript mqtt.demo.javascript

Client authentication

When a device is connected to a ApsaraMQ for MQTT broker to send and receive messages, you can use one of the following methods to authenticate the client. For more information, see Authentication overview.

Note If you use the signature authentication method to send and receive messages, you must configure the Username and Password parameters. For information about how to obtain the values of these parameters to verify signatures, see Signature authentication.

Programming language Download link
Java mqtt.token.mode.demo.java
PHP mqtt.token.mode.demo.php

Sample code for cloud SDKs

Note Cloud SDKs support only Java and Go.
Programming language Download link
Java MQTTConsumerDemo.java
MQTTProducerDemo.java
Go example.go