Mosquitto setup and configuration

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

  1. -d print debug information

  2. -f Use the content of the specified file as the content of the sent message

  3. -h specifies that the domain name to be connected defaults to localhost

  4. -i specifies to which clientId user to send a message

  5. -I specify to which clientId prefix users send messages

  6. -m message content

  7. -n send a null (null) message

  8. -p connection port number

  9. -q specifies the value of QoS (0,1,2)

  10. -t specifies topic

  11. -u Specify broker access user

  12. -P Specify the broker access password

  13. -V specifies the MQTT protocol version

  14. –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

  15. – Will-qos Will QoS value. This parameter needs to be used with --will-topic

  16. –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

  17. --Will-topic the topic where the user will send the Will message

mosquitto_sub command parameter description

  1. -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.

  2. -d print debug information

  3. -h specifies that the domain name to be connected defaults to localhost

  4. -i specify clientId

  5. -I specify clientId prefix

  6. -k keepalive Every time, send a PING message to notify the broker that it is still connected. The default is 60 seconds.

  7. -q specifies that you want to receive the message of QoS why the default QoS is 0

  8. -R does not display stale messages

  9. -t subscribe to topic

  10. -v print message

  11. –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

  12. – Will-qos Will QoS value. This parameter needs to be used with --will-topic

  13. –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

  14. --Will-topic the topic where the user will send the Will message

link
Published 17 original articles · Likes0 · Visits 220

Guess you like

Origin blog.csdn.net/neheqi/article/details/105343330