Article directory
overview
In the process of using python
and developing, MQTT
the message subscription mechanism is used to realize business requirements. One of them is that clients who subscribe to messages need to receive 上线与下线
push messages about other clients' MQTT servers. Here is a record of the implementation steps and precautions.
Operating system platform and software version :
- operating system:
Windows 10
- emqx version:
emqx 4.3
The main steps to realize are :
- Change the client subscription verification mechanism, that is,
是否允许匿名登录
from allow to disallow - Create an administrator user name and password, and give it the system instant message subscription permission (client log-off and offline notifications are system-level messages)
- Start the user name and password verification plug-in function
The main considerations :
emqx
The version difference (emqx 4.3
the version4.2
is different from the setting method of the previous version)- After the modification of the configuration file is completed, the eqmx server needs to be restarted to take effect
Operating procedures
emqx 4.2
and previous versions
emqx 4.2
How to change the login user name and password in previous versions can be retrieved through search engines. There are a large number of documents or technical blogs with instructions, so I won’t repeat them here.
emqx 4.3 version
Modify anonymous login authentication settings
Assuming that emqx is installed on D:\
the disk, the installation tutorial , at this time, open the system configuration file, located in d:\emqx\etc\emqx.conf
, open in text mode, modify the configuration item allow_anonymous
as false
, before modification, the system default value true
:
## Allow anonymous authentication by default if no auth plugins loaded.
## Notice: Disable the option in production deployment!
##
## Value: true | false
allow_anonymous = false
After modification, save and exit.
Open the login verification plugin
Log in 127.0.0.1:18083
, enter the default account name admin
and default password public
to log in to the console, click on the left navigation bar Plugins
(the Chinese interface shows is 插件
), and emqx_auth_mnesia
start
Before the Emqx4.3 version, there was an
emqx_auth_username
extension, which couldemqx_auth_username
modify its configuration by starting the plug-in module to verify the account and password.
ButEmqx 4.3
it is different after the version. The official document indicates thatemqx 4.3
in the versionemqx_auth_clientid
andemqx_auth_usernmae
is merged intoemqx_auth_mnesia
.emqx_auth_username
The module is henceforth obsolete.Emqx 4.3
Loading the plug-in in the versionemq_auth_username
will report an error that does not exist, so don't look for the installationemqx_auth_username
module anymore. Directly modifyemqx_auth_mnesia.conf
the module configuration file and add the account password.
Add username and password
Find emqx_auth_mnesia
the configuration file corresponding to the plug-in, located in D:\emqx\etc\plugins\emqx_auth_mnesia.conf
, add the following username and password:
## Password hash.
##
## Value: plain | md5 | sha | sha256 | sha512
auth.mnesia.password_hash = sha256
##--------------------------------------------------------------------
## ClientId Authentication
##--------------------------------------------------------------------
auth.client.1.clientid = admin
auth.client.1.password = your_own_password
##--------------------------------------------------------------------
## Username Authentication
##--------------------------------------------------------------------
## Examples:
auth.user.1.username = admin
auth.user.1.password = your_own_password
Save and exit.
Modify access control configuration files
Open d:\emqx\etc\acl.conf
, add a line of user access configuration:
{allow, {user, "admin"}, subscribe, ["$SYS/#"]}.
Allows admin
users to subscribe to system-level messages. Save and exit, restart emqx
the service
System message subscription for client online and offline
Client offline and online messages are system-level notifications, which require the client to subscribe to the system's predefined topic
:
-
offline topic
$SYS/brokers/+/clients/+/disconnected
-
online topic
$SYS/brokers/+/clients/+/connected
-
Online and offline topic
$SYS/brokers/+/clients/#
Sample code:
def on_connect(client, userdata, flags, rc):
# this method will be called when client connected to server successfully
# TODO do something when this client been notified about successfully connected to server
pass
def on_message(client, userdata, msg: mqtt.MQTTMessage):
# this method will be called when this client get a message under the topic(s) it subscribed, encluding the system message (I assuming you've already configure it properly)
# TODO do something when this client get a message
pass
mqtt_server = "127.0.0.1"
client = mqtt.Client("surveillance_client")
client.username_pw_set("admin", "aoto@123")
# 定义回调方法
client.on_connect = on_connect
client.on_message = on_message
# 600为keepalive的时间间隔
client.connect(mqtt_server, 1883, 600)
client.subscribe('surveillance', qos=0)
client.subscribe(r"$SYS/brokers/+/clients/#", qos=0)
# 启动mqtt消息订阅(非阻塞式)
client.loop_start()
After the above code runs, if other clients go online or offline, on_message()
the method is called back here, and the system log-off message is recovered, as follows:
客户端上线 topic: $SYS/brokers/[email protected]/clients/654321/connected
客户端上线消息报文 :
{
"username": "admin",
"ts": 1627476021893,
"sockport": 1883,
"proto_ver": 4,
"proto_name": "MQTT",
"keepalive": 600,
"ipaddress": "127.0.0.1",
"expiry_interval": 0,
"connected_at": 1627476021893,
"connack": 0,
"clientid": "654321",
"clean_start": true
}
客户端下线 topic: $SYS/brokers/[email protected]/clients/654321/disconnected
客户端下线消息报文 :
{
"username": "admin",
"ts": 1627476028659,
"reason": "tcp_closed",
"disconnected_at": 1627476028659,
"clientid": "654321"
}
Regular expressions can be used r"^\$SYS\/brokers\/.*\/clients\/.*\/(dis)?connected"
to match and filter online and offline messages for business logic processing.