Brief Book_Mosquitto Setup and Configuration
Eclipse Mosquitto is an open source message broker that implements MQTT protocol versions 3.1 and 3.1.1. Mosquitto is lightweight and suitable for all devices from low-power single-board computers to complete servers. The Mosquitto project also provides a C library for implementing the MQTT client and the very popular mosquitto_pub and mosquitto_sub command line MQTT client.
Other server proxy implementation: https://github.com/mqtt/mqtt.github.io/wiki/servers
Installation guidelines for each operating system: https://mosquitto.org/download/
1. Download and install
Take Ubuntu16 as an example
- Add to repository list
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
- Update packages
sudo apt-get update
- installation
sudo apt-get install mosquitto
- Install the command line client
sudo apt-get install mosquitto-clients
2. Configuration
2.1 The main configuration file mosquitto.conf
pid_file /var/run/mosquitto.pid
# Message
persistence true persistence true
persistence_location / var / lib / mosquitto /
# Log file
log_dest File / var / log / mosquitto / mosquitto . Log
# Other configuration
include_dir / etc / mosquitto / conf . D
# Disable anonymous access
allow_anonymous false
# authentication configuration
password_file / etc / mosquitto / pwfile
# permissions are configured
acl_file / etc / mosquitto / aclfile
2.2 Authentication configuration pwfile
- Create file if not
touch /etc/mosquitto/pwfile
- After the service is started, enter the following command and enter the password twice as prompted
mosquitto_passwd /etc/mosquitto/pwfile 用户名
2.3 Permission configuration aclfile
- open a file
vim /etc/mosquitto/aclfile
- Edit content
# 李雷只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题
user lilei
topic write test/#
topic read $SYS/#
Han Meimei can only subscribe to topics prefixed with test
user hanmeimei
topic read test/#
3. Start
-c: specify a specific configuration file to start
-d: run in the background
mosquitto -c /etc/mosquitto/mosquitto.conf -d
4. Test
Use the mosquitto_pub command for publishing and the mosquitto_sub command for subscription. Introduction of common parameters:
parameter | description |
---|---|
-h | Server host, default localhost |
-t | Specify subject |
-u | username |
-P | password |
-i | Client id, unique |
-m | Message content published |
subscription
mosquitto_sub -h localhost -t "test/#" -u hanmeimei -P 123456 -i "client1"
Subscription system topic
# 订阅客户端存活连接数
mosquitto_sub -h localhost –t '$SYS/broker/clients/active' -u lilei -P 123456 -i "client2"
release
mosquitto_pub -h localhost -t "test/abc" -u lilei -P 123456 -i "client3" -m "How are you?"
mosquitto_pub command parameter description
-
-d print debug information
-
-f Use the content of the specified file as the content of the sent message
-
-h specifies that the domain name to be connected defaults to localhost
-
-i specifies to which clientId user to send a message
-
-I specify to which clientId prefix users send messages
-
-m message content
-
-n send a null (null) message
-
-p connection port number
-
-q specifies the value of QoS (0,1,2)
-
-t specifies topic
-
-u Specify broker access user
-
-P Specify the broker access password
-
-V specifies the MQTT protocol version
-
–Will-payload specifies a message that is sent when the client and the broker accidentally disconnect. This parameter needs to be used with --will-topic
-
– Will-qos Will QoS value. This parameter needs to be used with --will-topic
-
–Will-retain Specifies that the Will message is treated as a retain message (that is, the message is retained after the message is broadcast). This parameter needs to be used with --will-topic
-
--Will-topic the topic where the user will send the Will message
mosquitto_sub command parameter description
-
-c Set 'clean session' to an invalid state, so that the subscription state is always maintained, even if the connection has been lost, if the connection is still connected, you can still receive the messages sent during the disconnection.
-
-d print debug information
-
-h specifies that the domain name to be connected defaults to localhost
-
-i specify clientId
-
-I specify clientId prefix
-
-k keepalive Every time, send a PING message to notify the broker that it is still connected. The default is 60 seconds.
-
-q specifies that you want to receive the message of QoS why the default QoS is 0
-
-R does not display stale messages
-
-t subscribe to topic
-
-v print message
-
–Will-payload specifies a message that is sent when the client and the broker accidentally disconnect. This parameter needs to be used with --will-topic
-
– Will-qos Will QoS value. This parameter needs to be used with --will-topic
-
–Will-retain Specifies that the Will message is treated as a retain message (that is, the message is retained after the message is broadcast). This parameter needs to be used with --will-topic
-
--Will-topic the topic where the user will send the Will message
link
- Project website: https://www.eclipse.org/paho
- Eclipse project information: https://projects.eclipse.org/projects/iot.paho
- GitHub:https://github.com/eclipse/paho.mqtt.java
- Use of MQTT Java client: https://www.jianshu.com/p/65e1748a930c
- Spring support: https://www.jianshu.com/p/6b60858b7d44