通过修改配置文件即可实现。
emq v1.1.3,rel\emqttd\etc\emqttd.config
‘subscription’扩展模块支持客户端上线时,自动订阅或恢复订阅某些主题(Topic):
%% Modules
{modules, [
%% Client presence management module.
%% Publish messages when client connected or disconnected
{presence, [{qos, 0}]},
%% Subscribe topics automatically when client connected
{subscription, [
%% $c will be replaced by clientid
%% {"$queue/clients/$c", 1},
{"$c/w", 1}
%% Static subscriptions from backend
%%backend
]}
%% Rewrite rules
%% {rewrite, [{file, "etc/rewrite.config"}]}
]},
emq v2.3.11,软件架构做了调整,把功能集成在了emq_modules模块,/data/loaded_plugins默认会加载emq_modules。
我们只需要改动配置文件emqx-rel-2.3.11/deps/emq_modules/etc/emq_modules.conf
##--------------------------------------------------------------------
## Subscription Module
##--------------------------------------------------------------------
## Enable Subscription Module.
##
## Value: on | off
module.subscription = on
## Subscribe the Topics automatically when client connected.
module.subscription.1.topic = %c/w
## Qos of the subscription: 0 | 1 | 2
module.subscription.1.qos = 1
把module.subscription设置为on,
把module.subscription.1.topic设置为%c/w,%c是通配符,会自动切换成clientId。注意是%c,而不是$c。