Mosquitto Build Android Push Service (ii) Mosquitto Introduction and construction

Source: Internet
Author: User
Tags install openssl

article steel to:

1. Understanding Mosquitto Server

2, build Mosquitto server in Liunx

3. Set Mosquitto cluster

I. Introduction of Mosquitto

An open-source message agent software that implements the messaging push protocol MQTT v3.1, which provides lightweight, support for a published/subscribed message push pattern, makes it easy to communicate short message communication between devices, such as the widely used low-power sensors, mobile phones, embedded computers, microcontrollers and other mobile devices. A typical application case is the remote monitoring and automation that Andy Stanford-clarkmosquitto (one of the founders of the MQTT Protocol) realizes at home. And in Oggcamp's speech, the MQTT protocol is elaborated in detail.

Mosquitto Pipe Network: http://mosquitto.org/

Mosquitto the latest source code: http://mosquitto.org/files/source/

Second, liunx under the installation of Mosquitto

Under Windows installation is relatively simple, in the Windows environment, you need to download the installation Cygwin, after the successful installation after the network download Mosquitto.exe after the installation can be clicked. There are many tutorials in the circle, it is not elaborated. And in the actual development environment using the Windows environment in a few, most of the use of Liunx, so the following is described under Liunx under the installation of Mosquitto.

Liunx Install the Mosquitto method in 2, download the installation package to the local installation as well as the Yum installation. It is recommended to install with Yum because it is more convenient, if the installation fails, consider using a compressed package installation.

Install with Yum:

1. The installation of Mosquitto requires OpenSSL support by default, so install OpenSSL first

Yum Install gcc gcc-c++

Yum Install Openssl-devel

Yum Install C-ares-devel

Yum Install Libuuid-devel

Yum Install wget

Yum Install CMake

Yum install build-essential python quilt devscripts python-setuptools Python3

Yum Install Libssl-dev libc-ares-dev uuid-dev daemon Openssl-devel

In the installation process if the server has installed the above software program, will not be installed, if a new version is found or not installed will prompt for updates and installation, user input ' Y ' to confirm the installation.

2. Download and compile the installation Mosquitto

Download the compressed package (up to now 2016-11 the latest compression pack is 1.4.10):

wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz

Extract:

TAR-XZVF mosquitto-1.4.9.tar.gz

Enter the directory and install:

CD mosquitto-1.4.9

Make

Make install 3. Configuring the Mosquitto.conf file

The most important file in the entire directory is mosquitto.conf, which is copied to the ECT

CP Mosquitto.conf/etc/mosquitto

Modify the Mosquitto.conf file

cd/etc/mosquitto/

To open a mosquitto.conf file by using the VI command

VI mosquitto.conf

Press "↓" to move the cursor to find the extra listeners

(You can use "/extra listeners" to find the text, press "N" key to continue to look over.) Press "I" key to enter edit state, press ESC to return. Exit is type ": Wq" to save the exit. Do not save forced exit ": q!")

Port number of the service

#port 1883 Change to Port 1883

Maximum number of connections, 1 means no limit

#max_connections-1 to Max_connections-1

Because my login user is root, I use root here

#use_username_as_clientid Change to User root

4. Start Mosquitto Server

Perform

Mosquitto-c/etc/mosquitto/mosquitto.conf

When the error occurs at startup, the dynamic library cannot be found when executing the Mosquitto client program: libmosquitto.so.1

Error message:./mosquitto_sub:error while loading shared libraries:libmosquitto.so.1:cannot open shared object File:no such fil E or directory

Solution, execute the command:

sudo ln-s/usr/local/lib/libmosquitto.so.1/usr/lib/libmosquitto.so.1

and Ldconfig.

If the following prompts indicate that the boot was successful, the warning is not.

5. Test server Subscription and release

Subscription services:

Mosquitto_sub-t Helloword

Mosquitto_sub Subscription Commands

-T Subscription topics

Helloworld theme name, can be arbitrarily named

Publish service:

Mosquitto_pub-t helloword-h localhost-m "Hello Word"

Mosquitto_pub issuing commands

-T Publishing topics

Helloworld Topic name

-H Host Address

LocalHost is published locally if the remote host writes the IP address directly (without the write port number)

-M Post information

What "Hello World" is publishing

If all goes well, you can successfully subscribe to a message as shown, and the information can be successfully published and the Subscriber can receive it.

Three, Mosquitto distributed cluster deployment

If you need to do a large amount of concurrency when you need to consider doing cluster processing, but I found in the data is not much, so I tidied up a simple mosquitto cluster mode.

First, the cluster requires more than 2 Mosquitto servers. Install the same way.

First understand the logical diagram of the next Mosquitto cluster mode, as follows:

As you can see, subscribers can receive published information regardless of whether they subscribe to the information on that server or publish information on that server. So next we set up the cluster server, in order to facilitate the demonstration of only 2 servers between the cluster building.

A proper noun for cluster deployment is called "Bridging," and implementing bridging requires modifying the CONFIG.MK and mosquitto.conf files. It is worth noting that if there are 10 servers to do the Mosquitto cluster, each server will open the bridge connection, and then only need to change the mosquitto.conf file on one server, the other server mosquitto.conf files do not need to make any changes. Greatly facilitates the maintenance of the cluster. If there is a new server join or delete only need to modify the master server mosquitto.conf.

1. Open the server Bridge connection

Go to the installation directory

CD mosquitto-1.4.9/

Open config.mk File

VI config.mk

Find With_bridge:=yes the Sign "#" to remove the Open bridge connection mode. (Default is on, for an error check)

2. Configuring Bridge connection properties for mosquitto.conf

Enter the ETC directory and open the mosquitto.conf file

cd/etc/mosquitto/

VI mosquitto.conf

Locate the Bridges node and add the following code below:

Connection MyTest
Address 10.19.22.53:1883
Topic room1/# both 2 sensor/myhouse/
Bridge_protocol_version mqttv311
Notifications True
Cleansession true
Try_private true
Start_type Automatic

---------------------------------------------------------------------------------

Connection connection name, you can take it anywhere

Address connection and port number, if you have more than one server, you can write multiple address

Topic theme name, "#" as a wildcard, indicating that the Publisher can follow any text in room1/

Both quality of service, 2 represents only once (you can view the details of the previous post for MQTT)

sensor/local prefix, can be arbitrarily named

myhouse/remote prefix representation, can be arbitrarily named

Bridge_protocol_version mqttv311 Bridge Connection protocol version MQTT3.11

Notifications whether to publish the bridged status information

Whether to purge messages from the remote server when the Cleansession bridge disconnects

Start_type Bridge mode, there are currently three kinds: automatic, lazy, once

Save and exit after setting up OK.

3. Turn on the server

The first step is to ensure that the server is turned on first, and the second step restarts the Mosquitto service of the primary server. If the configuration is correct, the primary server will automatically connect to all slave servers when it is turned on, as shown below:

As shown, the primary server is already bridged with the slave server.

4. Verify Publish/Subscribe

The characteristics of a cluster can be subscribed to and published on any server, and Subscribers can receive messages sent to any server.

Test scenario: When you subscribe to a message from the server and publish a message in the primary server, subscribers from the server can receive messages from the primary server.

(1) Type the command from the server:

Mosquitto_sub-t myhouse/room1/#注意: myhouse/is a remote prefix that writes topic in mosquitto.conf. room1/#是topic中的订阅主题 (2) Type the command in the primary server: Mosquitto_pub-t sensor/room1/temperature-m ' 26.3 ' Note: sensor/ is to write the local prefix for topic in mosquitto.conf. ROOM1/is a subscription subject temperature equivalent to "#" wildcard characters in topic If the mosquitto.conf configuration is correct and the local prefix is spelled correctly with the remote prefix, the message is displayed, indicating that the cluster configuration was successfully subscribed from the server, sent on the primary server, and received from the server Subscriber: The above is a simple cluster configuration. Next, develop the Mosquitto server for visual management applications.

Mosquitto Build Android Push Service (ii) Mosquitto Introduction and construction

Related Article

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.