0.1.2 • Published 4 months ago

eyu-egg-mqtt v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

eyu-egg-dubbo

NPM version npm download

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.

License

MIT

0.1.2

4 months ago

0.1.1

5 months ago

0.1.0

5 months ago