Based on the service side implementation Mosca
About Mosca, Mosca quoted from Home project development based on node.js, characteristics are described below:
Features
- MQTT 3.1 and 3.1.1 compliant.
- QoS 0 and QoS 1.
- Various storage options for QoS 1 offline packets, and subscriptions.
- As fast as it is possible.
- Usable inside ANY other Node.js app.
- Supports Node.js v0.10 and v0.12.
- Supports io.js v1.x and v2.x and v3.x (please do not use v3.1.0)
Of particular note is Mosca does not support Qos 2.
Why Mosca? First, based on node.js development, is relatively easy to get started, followed by the full protocol support, in addition to not support Qos 2, other basic support. Persistence support redis and mongo. Secondary development interface simple. Very simple to deploy, and support docker mirror.
Development steps
Installation Mosca
This article assumes that you have already installed environment node.js environment and redis, my native environment as follows: 10.11.2 node.js v0.12 redis latest version of mac ox.
Mosca Project Hosting Address: https://github.com/mcollina/mosca
official Wiki: https://github.com/mcollina/mosca/wiki
Installation is very simple, quoted official sources as follows:
Standalone
1 |
npm install mosca bunyan -g |
Embedded
1 |
npm install mosca --save |
Startup script
The official gave a simple example, the following is my test code now in use:
1 |
Mosca = var The require ( 'Mosca') |
Secondary development
You can listen to the list of events
clientConnected
: when a client is connected; the client is passed as a
parameter.clientDisconnecting
: when a client is being disconnected; the client is
passed as a parameter.clientDisconnected
: when a client is disconnected; the client is passed as
a parameter.published
: when a new message is published; the packet and the client are
passed as parameters.delivered
: when a client has sent back a puback for a published message; the packet and the client are
passed as parameters.subscribed
: when a client is subscribed to a topic; the topic and the
client are passed as parameters.unsubscribed
: when a client is unsubscribed to a topic; the topic and the
client are passed as parameters.
有了上面可以监听到事件你就可以根据自己的业务进行相应的开发,拦截特定的事件并添加业务代码
ascoltatore托管地址 https://github.com/mcollina/ascoltatori
高级参数设置可以参考 https://github.com/mcollina/mosca/wiki/Mosca-advanced-usage
权限验证可以参考 https://github.com/mcollina/mosca/wiki/Authentication-&-Authorization
配置ssl可以参考 https://github.com/mcollina/mosca/wiki/TLS-SSL-Configuration
配置WebSocket可以参考 https://github.com/mcollina/mosca/wiki/MQTT-over-Websockets
写在最后
感谢Mosca的作者mcollina,Mosca非常强大,并且足够简单。下篇文章会介绍如何利用IOS和Android开源客户端类库与Mosca对接,敬请期待!
转载: http://targe.me/2015/12/29/mqtt-second/