# Auto Subscribe

With Auto Subscription enabled, users can set multiple EMQX rules. After a client is successfully connected to EMQX, EMQX will complete the subscription process for the client automatically, and the clients no longer need to send SUBSCRIBE requests.

Before EMQX 5.0, this feature is called Proxy Subscription.

# Configuration

# Configuration Definition

FieldDefinitionRangeDefault
auto_subscribeAuto subscribe configurationstopicstopics
topicsSubscription OptionsSubscription configurations list. See Subscription Option[]

# Subscription Option

FieldDefinitionRangeDefault
topicRequired. TopicString, placeholders supportedNo default value
qosNot Required. Subscription QoS0 or 1 or 2. Refer to the MQTT QoS definition0
rhNot Required. MQTT version 5.0. Whether to send retain message when a subscription is created.0: Not send the retain message
1: Send the retain message
0
rapNot Required. MQTT version 5.0. When forwarding messages, Whether to send with retain flag0: Set retain 0
1: Keep retain flag
0
nlNot Required. MQTT version 5.0. Whether the message can be forwarded to the client when published by itself0: Forwarded to self
1: Not forwarded to self
0

# Subscription Placeholders

PlaceholderDefinition
${clientid}Client ID
${username}Client Username
${ip}Client TCP connection local IP address
${port}Client TCP connection local Port

# Quick Start

Add the following configuration items to the configuration file

auto_subscribe {
    topics = [
        {
            topic = "c/${clientid}"
        },
        {
            topic = "client/${clientid}/username/${username}/host/${host}/port/${port}"
            qos   = 1
            rh    = 0
            rap   = 0
            nl    = 0
        }
    ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
+---------------------------+             +----------------+
| clientid: demo_client1    |             |  EMQX Broker   |
| username: admin | | |
| local host: 192.168.1.234 | <---------> |                |
| local port: 55678         |             |                |
+---------------------------+             +----------------+
1
2
3
4
5
6

When the client uses versions lower than 5, the following subscriptions are available after connection.

topic: c/demo_client1
qos: 0
1
2
topic: client/demo_client1/username/admin/host/192.168.1.234/port/55678
qos: 1
1
2

When the client uses version 5, the following subscriptions are available after connection.

topic: c/demo_client1
qos: 0
rh: 0
rap: 0
nl: 0
1
2
3
4
5
topic: client/demo_client1/username/admin/host/192.168.1.234/port/55678
qos: 1
rh: 0
rap: 0
nl: 0
1
2
3
4
5