All Products
Search
Document Center

ApsaraMQ for MQTT:P2P messaging model

Last Updated:Mar 11, 2026

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

DimensionPub/SubP2P
TopologyOne-to-many or many-to-manyOne-to-one
Topic agreementSender and receiver must agree on a topic in advanceSender specifies the receiver's client ID directly in the topic
SubscriptionReceiver must subscribe to the topic before receiving messagesNo subscription needed -- the receiver only needs to complete initialization
Typical use caseBroadcasting updates to multiple consumersSending a targeted command or notification to a specific device

Topic format

P2P messages use a three-level topic structure:

{parentTopic}/p2p/{receiverClientID}
SegmentDescriptionExample
{parentTopic}A first-level topic that your MQTT client has permission to publish tomytopic
p2pA 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_001

Send 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

Eclipse Paho .NET SDK

.NET sample code

C

Eclipse Paho C SDK

C sample code

Java

Eclipse Paho SDK

Java sample code

JavaScript

Eclipse Paho JavaScript

JavaScript sample code

Python

Eclipse Paho Python SDK

Python sample code

PHP

Mosquitto-PHP

PHP sample code

Note

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.