0.1.2 • Published 4 months ago
eyu-egg-mqtt v0.1.2
eyu-egg-dubbo
Install
$ npm i eyu-egg-mqtt --save
Usage
Register with the plugin list
// {app_root}/config/plugin.js
exports.eyuEggMQTT = {
enable: true,
package: 'eyu-egg-mqtt',
};
Configuration
// {app_root}/config/config.default.js
exports.mqtt = {
client: {
host: 'xxx.xxx.xxx.xxx',
port: 1883,
username: 'xxx',
password: 'xxxxxxx',
keepalive: 60,
cleanup: true
}
};
see config/config.default.js for more detail.
Example
Configuring routes
// app/route.js
module.exports = app => {
const { mqtt, router, controller } = app;
router.get('/', controller.home.index);
// subscribe topic
mqtt.route('/topic/:uuid/sub', controller.mqtt.test);
};
Handler/Controller
// app/controller/mqtt
const { Controller } = require('egg');
class MQTTController extends Controller {
async test() {
const { app, ctx } = this;
// params in topics
const { uuid } = ctx.params
// json payload
const { foo, bar } = ctx.request.body
// publish message
await app.pub('pub_topic', JSON.stringify({
foo: 1,
bar: 0
}))
}
}
module.exports = MQTTController
Questions & Suggestions
Please open an issue here.