ApsaraMQ for MQTT supports a point-to-point (P2P) messaging model in addition to the standard publish/subscribe (Pub/Sub) model defined by the MQTT protocol. P2P lets a sender deliver a message directly to a specific client without requiring the receiver to subscribe to a topic first.
Why use P2P
In a standard Pub/Sub workflow, one-to-one messaging requires the sender and receiver to agree on a shared topic in advance. The receiver must actively subscribe to that topic before any messages arrive. This adds overhead to both the application logic and the subscription registry.
P2P removes these constraints:
No pre-agreed topic -- The sender targets the receiver by client ID. Neither side needs to coordinate on a topic name ahead of time.
No subscription required -- The receiver gets P2P messages as soon as it connects and completes initialization. No explicit subscribe call is needed.
Lower push latency -- The messaging link is optimized separately for P2P delivery, reducing end-to-end latency compared to Pub/Sub fan-out.
P2P compared to Pub/Sub
| Dimension | Pub/Sub | P2P |
|---|---|---|
| Topology | One-to-many or many-to-many | One-to-one |
| Topic agreement | Sender and receiver must agree on a topic in advance | Sender specifies the receiver's client ID directly in the topic |
| Subscription | Receiver must subscribe to the topic before receiving messages | No subscription needed -- the receiver only needs to complete initialization |
| Typical use case | Broadcasting updates to multiple consumers | Sending a targeted command or notification to a specific device |
Topic format
P2P messages use a three-level topic structure:
{parentTopic}/p2p/{receiverClientID}| Segment | Description | Example |
|---|---|---|
{parentTopic} | A first-level topic that your MQTT client has permission to publish to | mytopic |
p2p | A fixed keyword that marks this message as P2P (set as the second-level topic) | p2p |
{receiverClientID} | The client ID of the intended receiver (set as the third-level topic) | GID_xxxx@@@DEVICEID_001 |
The assembled topic looks like this:
mytopic/p2p/GID_xxxx@@@DEVICEID_001Send P2P messages
From an MQTT SDK
Construct the P2P topic by appending /p2p/{receiverClientID} to your parent topic, then publish to it.
Java
// Build the P2P topic: parentTopic + /p2p/ + receiver's client ID
String p2pTopic = topic + "/p2p/GID_xxxx@@@DEVICEID_001";
sampleClient.publish(p2pTopic, message);From an ApsaraMQ for RocketMQ SDK
In the ApsaraMQ for RocketMQ SDK, parent topics and subtopics are configured separately. Set the MqttSecondTopic user property to the P2P subtopic string.
Java
// Set the subtopic to target a specific MQTT client
String subTopic = "/p2p/GID_xxxx@@@DEVICEID_001";
msg.putUserProperties(PropertyKeyConst.MqttSecondTopic, subTopic);SDK downloads and sample code
Download an SDK and view sample code for sending P2P messages in each supported language.
Language | SDK | Sample code |
.NET | ||
C | ||
Java | ||
JavaScript | ||
Python | ||
PHP |
The Go SDK does not support sending or receiving P2P messages.
Receive P2P messages
No subscription is required. Complete the standard MQTT client initialization, and the client automatically receives any P2P message addressed to its client ID.