Mobile Mosquitto Brief Tutorial (Install/use/test)

Source: Internet
Author: User
In the article "Android Mainstream Push program analysis (MQTT/XMPP/GCM)", we have introduced how to use Smart Message Transfer Protocol MQTT to complete the message push in the mobile domain, finally also mentioned the open source project Mosquitto. In fact, Mosquitto is a proxy server that implements the MQTT3.1 protocol, developed by Andy Stanford-clark, one of the founders of the MQTT protocol, which provides us with a very good solution for lightweight data exchange. The purpose of this article is to record the installation and use of Mosquitto services for future inspection.

1. Access & Installation

Mosquitto provides versions of Windows, Linux, and QNX systems, and the installation files are available from http://mosquitto.org/files/binary/addresses (the latest 1.1.x version is recommended). The installation process under Windows System is very simple, we can even install the Mosquitto directly into the system service, but in the actual application, we prefer to use the Linux system server, next we will focus on the Linux version of Mosquitto installation methods.

Install Mosquitto on Linux system, I suggest you use the source code installation mode, the newest source code can obtain from http://mosquitto.org/files/source/address. After decompression, we can find the main configuration file in the source directory config.mk, which contains all the Mosquitto installation options, detailed parameters described below:
# whether the Tcpd/libwrap feature is supported.
#WITH_WRAP: =yes

# Open SSL/TLS support
#WITH_TLS: =yes

# whether to open Tls/psk support
#WITH_TLS_PSK: =yes

# Comment Out to disable client client threading support.
#WITH_THREADING: =yes

# Use a strict protocol version (old version compatibility is problematic)
#WITH_STRICT_PROTOCOL: =yes

# Whether to open bridging mode
#WITH_ Bridge:=yes

# Whether to turn on the persistence function
#WITH_PERSISTENCE: =yes

# Whether to monitor the running status
#WITH_MEMORY_TRACKING: =yes
It should be noted that, by default, Mosquitto installation requires OpenSSL support, and if SSL is not required, some of the config.mk-related options (WITH_TLS, WITH_TLS_PSK) will need to be turned off. Next, you run make install to install, and when you're done, you'll find Mosquitto, mosquitto_passwd, Mosquitto_pub, and mosquitto_sub four tools (screenshots below) on the system command line to start the agent, respectively. , manage passwords, publish messages, and subscribe to messages.



2. Configuration & Operation

After the installation is complete, all configuration files are placed in the/etc/mosquitto/directory, the most important of which is the Mosquitto configuration file, mosquitto.conf, which is described in detail in the configuration parameters below.
# ================================================================= # General Configuration # ====================== =========================================== # Client heartbeat interval #retry_interval 20 # System State Refresh Time #sys_interval 10 # System Resource recovery time, 0 table
As soon as possible processing #store_clean_interval 10 # service Process PID #pid_file/var/run/mosquitto.pid # Service Process System User #user Mosquitto # client Heartbeat message Maximum concurrent number #max_inflight_messages 10 # Client Heartbeat message Cache queue #max_queued_messages 100 # used to set the expiration time for client long connections, which never expire by default #persistent_client_ Expiration # ================================================================= # Default Listener # ================= ================================================ # Service-bound IP address #bind_address # service-bound port number #port 1883 # Maximum number of connections allowed, 1 indicates no limit #m 

AX_CONNECTIONS-1 # CAFILE:CA certificate File # CAPATH:CA Certificate # CERTFILE:PEM Certificate # KEYFILE:PEM key file #cafile #capath #certfile #keyfile # A certificate must be provided to ensure data security #require_certificate false # If the Require_certificate value is true,use_identity_as_username, it must also be true #use_ Identity_as_username false # Enable PSK (Pre-shared-key) support
#psk_hint # SSL/TSL Encryption algorithm, you can use the "OpenSSL ciphers" command to obtain the # as the output of that command. #ciphers # ================================================================= # persistence # ======================= ========================================== # Messages are automatically saved at intervals #autosave_interval 1800 # Message automatic save function switch #autosave_on_changes False # Switch Persistence true # Persistent db file #persistence_file mosquitto.db # Persistent DB file directory #persistence_location/var/lib/m osquitto/# ================================================================= # Logging # ========================== ======================================= # 4 Log modes: stdout, stderr, syslog, topic # None means no log, this configuration can promote a little bit of performance log_dest None # Select the level of the log (multiple items can be set) #log_type error #log_type warning #log_type Notice #log_type information # whether to log client connection information #connection_message s true # whether log time is logged #log_timestamp true # ================================================================= # security # = ================================================================ # prefix for client IDRestrictions that can be used to guarantee security #clientid_prefixes # Allow anonymous users to #allow_anonymous true # User/password file, default format: Username:password #password_file # PSK format password  File, default format: Identity:key #psk_file # pattern Write Sensor/%u/data # ACL permissions configuration, common syntax is as follows: # User restrictions: Users <username> # Topic Restrictions: Topic [Read|write] <topic> # Regular limit: pattern write sensor/%u/data #acl_file # =========================================== ====================== # Bridges # ================================================================= # allows "bridging" to be used between services Mode (available for distributed deployment) #connection <name> #address Finally, starting the Mosquitto service is simple, run the command line directly
mosquitto-c/etc/mosquitto/mosquitto.conf-d"The service can be opened. Next, let's enjoy the powerful features of Mosquitto. Of course, with the Mosquitto, we can safely abandon the "humble" rsmb, interested, we can also try to use the Mosquitto service to the previous Android push service.


In addition, Mosquitto is an asynchronous IO framework that is tested to easily handle more than 20,000 client connections. Of course, the actual maximum load capacity and the complexity of the business has a relatively large relationship. The figure below is the result of the stress test I carried out on an ordinary Linux machine, which we can refer to.



Friendly reminder:When testing, do not forget to adjust the maximum number of connections and stack size of the system, such as: Linux can be used ulimit-n20000-s512 command to set the system parameters you need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.