Auto Subscribe
TIP
Auto Subscribe is not available in Serverless deployments.
Auto Subscribe is an enhanced MQTT feature supported by the EMQX Platform. When the auto subscription function is enabled, users can automatically subscribe clients to predefined topics upon successful connection. This eliminates the need for clients to send explicit SUBSCRIBE
requests, as the EMQX Platform handles the subscription process automatically.
Prior to EMQX 5.0, this feature was known as Proxy Subscription.
Configure Auto Subscribe in Console
Go to your deployment in the EMQX Platform Console. In the left menu, click Monitor -> Auto Subscribe.
On the Auto Subscribe page, click the + Add button at the upper right corner.
In the Add Auto Subscription dialog, type the test topic
a/1
in the Topic text box. Leave other settings as default.- Topic: Type the topic to which the client will automatically subscribe. You can dynamically build the topic using placeholders. For details, see Placeholders.
- QoS: Specify the quality of service of the topic. Options:
0
,1
, and2
. - No local: Options:
False
orTrue
. - Retain as Published: Specify if the message sent with the specified topic will be retained. Options:
False
orTrue
. - Retained Handling: Options:
0
,1
, and2
.
Click the Confirm button. The auto subscribe topic
a/1
is created successfully.
Now, the auto subscription function is enabled. New subscribers will subscribe to the topic a/1
automatically once they are connected to the EMQX Platform.
Try Auto Subscription Using MQTTX
Topic a/1
is configured as the auto-subscribe topic in Configure Auto Subscribe in Platform Console. The following procedure demonstrates how a client subscribes to the topic a/1
automatically once it is connected to the EMQX Platform.
Open the MQTTX Web.
Click the New Connection to create a client connection as a publisher. Configure the following fields:
- Name: Enter
Demo
. - Host: Enter the address in the MQTT Connection Information section on your deployment Overview page.
- Port: Enter
8084
. - Username and Password: Configure the authentication information in your deployment's Access Control -> Authentication page and enter the credentials here.
Leave other settings as default and click Connect.
- Name: Enter
Create another MQTT client connection named
Subscriber
.Select client
Demo
in the Connections pane. Entera/1
as the topic. Send a message to this topic.The client
Subscriber
receives the message automatically without creating a new subscription.The client
Demo
also receives the message as it is also a new connection.TIP
In the publish/subscribe pattern, a client can be both sender and subscriber.
Go to the EMQX Platform Console. From the left menu, click Monitor -> Subscriptions. You can see two subscriptions automatically subscribe to topic
a/1
.
Placeholders
Auto Subscribe supports placeholders to dynamically build topics. The format of the placeholder is ${}
. The variables supported by the placeholder are:
${clientid}
: Client ID.${username}
: Client username.${host}
: IP address when the client connects to the EMQX Platform.
For example, when the client ID is emqx_c
and the configured topic is a/${clientid}
, the client will automatically subscribe to the topic a/emqx_c
after connecting to EMQX Platform.