Brief Mosquitto tutorial
1. obtain and install
Mosquitto provides Windows, Linux, and qnx versions. The installation file can be obtained from http://mosquitto.org/files/binary. In Windows, the installation process is very simple. You can even directly install Mosquitto as a system service. However, in actual applications, you are more inclined to use Linux servers. Install Mosquitto on a Linux system. We recommend that you use the source code installation mode. The latest source code can be obtained from http://mosquitto.org/files/source. After decompression, you can find the main configuration file config. mk in the source code Directory, which contains all Mosquitto installation options. The detailed parameters are described as follows:
# Whether tcpd/libwrap is supported.
# WITH_WRAP: = yes
# Whether to enable SSL/TLS support
# WITH_TLS: = yes
# Whether TLS/PSK support is enabled
# WITH_TLS_PSK: = yes
# Comment out to disable client threading support.
# WITH_THREADING: = yes
# Whether to use a strict protocol version (old version compatibility may be a problem)
# WITH_STRICT_PROTOCOL: = yes
# Whether to enable the Bridge Mode
# WITH_BRIDGE: = yes
# Whether to enable the persistence Function
# WITH_PERSISTENCE: = yes
# Whether to monitor the running status
# WITH_MEMORY_TRACKING: = yes
Note that by default, the installation of Mosquitto requires OpenSSL support. If SSL is not required, disable config. some SSL-related options (WITH_TLS, WITH_TLS_PSK) in mk ). Then, run make install to install the tool. After installation, four tools, mosquitto, mosquitto_passwd, mosquitto_pub, and mosquitto_sub, will be found in the system command line, it is used to start the proxy, manage the password, publish messages, and subscribe messages respectively.
Ii. configuration and operation
After the installation is complete, all the configuration files will be placed in the/etc/mosquitto/directory, the most important of which is the Mosquitto configuration file, that is, mosquitto. conf. The following describes the configuration parameters.
#===================================================== ======================================
# General configuration
#===================================================== ======================================
# Interval of client heartbeat
# Retry_interval 20
# Refresh time of system status
# Sys_interval 10
# Recovery time of system resources. 0 indicates processing as soon as possible
# Store_clean_interval 10
# Service process PID
# Pid_file/var/run/mosquitto. pid
# System users of service processes
# User mosquitto
# Maximum number of concurrent client heartbeat messages
# Max_inflight_messages 10
# Client Heartbeat message cache queue
# Max_queued_messages 100
# It is used to set the expiration time of the client's persistent connection, which never expires by default.
# Persistent_client_expiration
#===================================================== ======================================
# Default listener
#===================================================== ======================================
# IP address bound to the service
# Bind_address
# Port number bound to the service
# Port 1883
# The maximum number of connections allowed.-1 indicates no limit.
# Max_connections-1
# Cafile: CA certificate file
# Capath: CA certificate directory
# Certfile: PEM Certificate file
# Keyfile: PEM key file
# Cafile
# Capath
# Certfile
# Keyfile
# Certificates must be provided to ensure data security
# Require_certificate false
# If the require_certificate value is true, use_identity_as_username must also be true
# Use_identity_as_username false
# Enable PSK (Pre-shared-key) Support
# Psk_hint
# The SSL/TSL encryption algorithm can be obtained using the "openssl ciphers" command.
# As the output of that command.
# Ciphers
#===================================================== ======================================
# Persistence
#===================================================== ======================================
# Interval between automatic message storage
# Autosave_interval 1800
# Switch of the automatic message Saving Function
# Autosave_on_changes false
# Persistence function switch
Persistence true
# Persistent DB files
# Persistence_file mosquitto. db
# Persistent DB file directory
# Persistence_location/var/lib/mosquitto/
#===================================================== ======================================
# Logging
#===================================================== ======================================
# Four log modes: stdout, stderr, syslog, and topic
# None indicates that logs are not recorded. This configuration can improve performance.
Log_dest none
# Select the log level (multiple levels can be set)
# Log_type error
# Log_type warning
# Log_type notice
# Log_type information
# Whether to record client connection information
# Connection_messages true
# Whether to record the log time
# Log_timestamp true
#===================================================== ======================================
# Security
#===================================================== ======================================
# Client ID prefix restriction, which can be used to ensure security
# Clientid_prefixes
# Allow Anonymous Users
# Allow_anonymous true
# User/password file, default format: username: password
# Password_file
# Password file in PSK format. Default format: identity: key
# Psk_file
# Pattern write sensor/% u/data
# ACL permission configuration. Common Syntax:
# User restrictions: user <username>
# Topic restrictions: topic [read | write] <topic>
# Regular Expression restrictions: pattern write sensor/% u/data
# Acl_file
#===================================================== ======================================
# Bridges
#===================================================== ======================================
# Allow "bridging" between services (for distributed deployment)
# Connection <name>
# Address
# Topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
# Set the bridge Client ID
# Clientid
# Whether to clear messages on the remote server when the bridge is disconnected
# Cleansession false
# Whether to publish bridge status information
# Events true
# Set the topic address to which the message will be published in bridging mode.
# $ SYS/broker/connection/<clientid>/state
# Icationication_topic
# Set the keepalive value of the bridge.
# Keepalive_interval 60
# Bridge Mode. Currently, there are three types: automatic, lazy, and once
# Start_type automatic
# Time-out period of IC in Bridge Mode
# Restart_timeout 30
# Time-out period of the Bridge Mode lazy
# Idle_timeout 60
# User Name of the bridge Client
# Username
# Bridging client Password
# Password
# Bridge_cafile: bridge the CA certificate file of the Client
# Bridge_capath: Specifies the CA certificate directory of the bridge client.
# Bridge_certfile: bridge the client's PEM Certificate file
# Bridge_keyfile: bridge the client's PEM key file
# Bridge_cafile
# Bridge_capath
# Bridge_certfile
# Bridge_keyfile
# Put your configuration in the following directory:
Include_dir/etc/mosquitto/conf. d
It is easy to start the Mosquitto service. Simply run the command line "mosquitto-c/etc/mosquitto. conf-d. In addition, Mosquitto is a pure asynchronous IO framework. It has been tested to easily process more than 20000 client connections. Of course, the actual maximum carrying capacity is also closely related to the business complexity. Do not forget to adjust the maximum number of connections and stack size during the test. For example, you can use the ulimit-n20000-s512 command on Linux to set the system parameters you need.
This article permanently updates the link address: