MQTT for Transport and Sesssions
EMQ is an open-source MQTT broker implementation by Feng Lee. It's clean, concise and robust. We've developed an EMQ compatible plugin that works as plugin and have different connection schema than Cowboy's ranch integration of N2O.
Usually, Web Framework uses HTTP or WebSocket endpoints. But what if we need to connect application on devices with TCP/UDP transports? One of the protocols addressing this usecase is MQTT that was designed by Andy Stanford-Clark (IBM) and Arlen Nipper in 1999 for connecting Oil Pipeline telemetry systems over satellite. The protocol is designed to work over connectionless unreliable environments, and provides three levels of QoS delivery: at least once, at most once, exactly once.
Starting from 4.5, N2O reduces everything that duplicates MQTT features. On the other hand, MQTT version is a completely N2O-compatible embeddable protocol relay with limited features. And it takes only 25KB in Erlang.
- n2o — N2O Protocol Server version 4.5 MQTT — 10KB
- n2o_ftp — N2O File Protocol: FTP — 4KB
- n2o_nitro — N2O Nitro Protocol: PICKLE, FLUSH, DIRECT, IO, INIT — 3KB
- n2o_proto — N2O Protocols Loop: NITRO, FTP — 1KB
- n2o_secret — N2O Security: HMAC AES/CBC-128 — 1KB
N2O MQTT provides real smooth experience for developers: clean git history, small codebase, nitro compatible, man/html docs.
Differences from WebSocket
N2O_start and n2o.js are no longer used in MQTT version of N2O. Instead of N2O_start, MQTT_start and mqtt.js should be used for session control replacement. We traded HEART protocol and session facilities for built-in MQTT features. N2O authentication and authorization mechanisms are also abandoned as MQTT could provide AUTH features too.
- n2o_session — Server Cookies
- n2o_stream — XHR fallback
- n2o_heart — PING protocol
- n2o_gproc — GPROC
- n2o_syn — SYNC
- mq.js — MQTT_start
- ranch — esockd
- cowboy — mochiweb
Endpoints
All N2O/MQTT messages which go directly to TCP and/or WebSocket. However there are some endpoints which are not TCP sockets, even non-sockets, like gen_server endpoint, HTTP REST endpoint, which is non-WebSocket endpoint, and there is a room for other endpoint types.
Here is a list of types of endpoints which are supported by EMQ and accesible to N2O apps: WebSockets, MQTT, MQTT-SN, TCP, UDP, CoAP. Normal use of N2O as a Web Framework or a Web Application Server is through WebSockets, but for IoT and MQTT applications it could be served through UDP or SCTP protocols providing application level message delivery consistency. By using MQTT as a transport we extend the supported set of endpoint protocols.
Also N2O is able to work under cowboy Erlang web server and mochiweb web server. There is rest library to deal with REST endpoint.
N2O and MQTT Formatters
N2O supports TEXT, JSON, XML, BERT, MessagePack formatters but you can add your own implementations either. They are terminal formatters before socket interface library (usually ranch or esockd). For example one may format IO message with BERT encoding n2o:format({io,Eval,Data},bert).
MQTT format messages only in MQTT format. You can wrap binary B in MQTT packet as emqttd_message:make(Name, 0, Name, B).
Sample Application
MQTT JavaScript Client
Enable EMQ Plugin
The N2O over MQTT bridge is a drop-in plugin for EMQ broker. After starting the server you should enable N2O over MQTT bridge EMQ plugin on plugin page http://127.0.0.1:18083/#/plugins and then open the application sample http://127.0.0.1:8000/spa/login.htm
The nice thing about EMQ is that it enables session introspection for N2O connections. The EMQ Dashboards is written in vue.js and a bit complex for admin application. The possible and expecting hackaton is on rewriting EMQ admin with hyperapp and n2o. The IBM library Paho is also too old and fat to fit N2O sizes. Some parts should be replaced with more modern and compact pinger and connection respawning from n2o.js. This is expecting further research and imporevement.
EMQ — Lightweight Broker as Service Container for N2O Apps
EMQ is the latest software that has robust iOS and Android MQTT client libraries, just have a look at https://github.com/emqtt Github organization.
We made a compatible EMQ plugin and N2O Review sample application with built-in EMQ with minimal dependencies. Our fork is based on the latest EMQ master and is dedicated for building with our MAD build tool.