In addition to the publish-subscribe (Pub/Sub) messaging model supported by the standard Message Queuing Telemetry Transport (MQTT) protocol, ApsaraMQ for MQTT also supports the point-to-point (P2P) messaging model. This topic describes the concept and principles of the P2P model. It also describes how to use ApsaraMQ for MQTT to send and receive P2P messages.
What is the P2P model?
P2P is a messaging model used for one-to one messaging scenarios, where only one message sender and one message receiver exist. The Pub/Sub model is typically used for one-to-many or many-to-many messaging scenarios, where one or more message senders and multiple message receivers exist.
In the P2P model, the sender specifies the intended receiver when sending a message and ensures that the message is consumed only by a specific client. The sender directly specifies the receiver in the topic information when sending a message, and the receiver can receive the message without subscribing to the topic in advance.
The P2P model not only reduces the cost of subscription registration for receivers but also decreases push latency because the messaging link is separately optimized.
Messaging using the P2P model vs. Messaging using the Pub/Sub model
The differences between messaging in ApsaraMQ for MQTT using the P2P model and the Pub/Sub model are as follows:
Message sending: In the Pub/Sub model, the sender must send messages to a topic agreed on with the receiver. In the P2P model, the sender does not need to agree on a topic for message transmission in advance. The sender can directly send messages to the receiver based on the conventions.
Message receiving: In the Pub/Sub model, the receiver must subscribe to a topic agreed on with the sender in advance to receive messages. In the P2P model, the receiver can receive messages without subscribing to a topic in advance. This simplifies the receiver's program logic and reduces subscription costs.
Send P2P messages
When using an ApsaraMQ for MQTT SDK to send P2P messages, you need to set the second-level topic to p2p and the third-level topic to the client ID of the receiver.
Sample code in Java
String p2pTopic =topic+"/p2p/GID_xxxx@@@DEVICEID_001";
sampleClient.publish(p2pTopic,message);When using an ApsaraMQ for RocketMQ SDK to send P2P messages, you only need to set the subtopic property to the subtopic string in the previous sample code because parent topics and subtopics are separately configured.
Sample code in Java
String subTopic="/p2p/GID_xxxx@@@DEVICEID_001";
msg.putUserProperties(PropertyKeyConst.MqttSecondTopic, subTopic);The following table provides links for downloading SDKs for different programming languages and sample code for sending P2P messages.
Language | SDK download | Sample code |
.NET | ||
C | ||
Java | ||
JavaScript | ||
Python | ||
PHP |
The SDK for Go does not support the sending or receiving of P2P messages.
Receive P2P messages
The client that receives messages only needs to complete the initialization before receiving P2P messages.