All Products
Search
Document Center

ApsaraMQ for MQTT:P2P messaging model

Last Updated:Jun 16, 2026

Beyond the publish-subscribe (Pub/Sub) messaging model of standard MQTT, ApsaraMQ for MQTT supports point-to-point (P2P) messaging, which delivers messages directly to a specific client without requiring topic subscriptions. Learn the concepts, principles, and usage of P2P messaging in ApsaraMQ for MQTT.

What is the P2P model?

P2P is a one-to-one messaging model with a single sender and a single receiver. In contrast, the Pub/Sub model is designed for one-to-many or many-to-many scenarios with multiple receivers.

In the P2P model, the sender specifies the receiver in the topic when sending a message, ensuring that only the intended client consumes it. The receiver does not need to subscribe to the topic in advance.

The P2P model eliminates subscription registration overhead and reduces push latency through a separately optimized messaging link.

Messaging using the P2P model vs. Messaging using the Pub/Sub model

The differences between P2P and Pub/Sub messaging in ApsaraMQ for MQTT:

  • Message sending: In the Pub/Sub model, the sender must send messages to a pre-agreed topic. In the P2P model, no pre-agreed topic is required. The sender directly sends messages to the receiver.

  • Message receiving: In the Pub/Sub model, the receiver must subscribe to a pre-agreed topic to receive messages. In the P2P model, the receiver can receive messages without subscribing to a topic, which simplifies program logic and reduces subscription costs.

Send P2P messages

To send P2P messages with an ApsaraMQ for MQTT SDK, 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, set the subtopic property to the subtopic string shown in the previous sample code. Parent topics and subtopics are configured separately.

Sample code in Java

String subTopic="/p2p/GID_xxxx@@@DEVICEID_001";
msg.putUserProperties(PropertyKeyConst.MqttSecondTopic, subTopic);

The following table lists SDK download links and P2P message sending sample code for each supported language.

Language

SDK download

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 SDK for Go does not support the sending or receiving of P2P messages.

Receive P2P messages

The receiver only needs to complete initialization to start receiving P2P messages.