mosquitto note
is based on MQTT message push [ https://mosquitto.org ]
Purpose
* The problem to be solved is: push messages to Android phones * Do not use third-party SDKs, such as Jiguang Push, Baidu Cloud Push, and Carrier Pigeon. * Self-built message push background
module
* Agent (relay message, daemon) mosquitto * Send message client mosquitto_pub * Receive message client mosquitto_sub * Manage password file mosquitto_passwd
Ubuntu cheap
* sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa * sudo apt-get update * sudo apt-get install mosquitto mosquitto-clients * Start chkconfig mosquitto on boot
test
* First start the service process mosquitto -d * Subscribe to a channel (news) message (listen) mosquitto_sub -t news * Send a message (hello) to a channel (news) mosquitto_pub -t news -m "hello" * It can be observed that after mosquitto_pub, you can see the message just sent in mosquitto_sub
configure
* Configuration file path /etc/mosquitto/mosquitto.conf * Start the service process mosquitto, in addition to possible command line parameters, but also through the configuration file, the configuration file is more detailed. * You can specify ip address and port, you can also use username and password * Can add encryption certificate
case
use password
* Create a read-only user user1 and a writable user user2 with a password of user1234
** mosquitto_passwd -c /etc/mosquitto/pawd user1 // -c 表示新增一个文件,如果创建第二个用户,不需要-c
** mosquitto_passwd /etc/mosquitto/pawd user2
* 修改配置文件,不允许匿名用户,指定存储用户名和密码的文件路径,和用户读写权限的文件
** 创建自己的配置文件, touch /etc/mosquitto/conf.d/my.conf
```
allow_anonymous false
password_file /etc/mosquitto/pawd
acl_file /etc/mosquitto/acl
```
** 创建用户读写权限的文件/etc/mosquitto/acl
```
user user1
topic read news
user user2
topic write news
```
* 测试
** sudo service mosquitto restart
** mosquitto_sub -t news -u user1 -P user1234
** mosquitto_pub -t news -m "hello" -u user2 -P user1234