hinos-mqtt

Module help to connect via mqtt for hinos

Usage no npm install needed!

<script type="module">
  import hinosMqtt from 'https://cdn.skypack.dev/hinos-mqtt';
</script>

README

hinos-mqtt

A plugin helps to manage mqtt connection for hinos

Install

npm i hinos-mqtt -S

Examples

Typescript

Config mongo information

import { Mqtt } from 'hinos-mqtt';

Mqtt({
  url: 'mqtt://127.0.0.1',
  releaseTimeout: 60000,
  topic: {
    'API_RECEIVER': {
      des: 'Listen queue message for API',
      getListenerName: (_data: any) => `${AppConfig.mqtt.queue.API_RECEIVER}`,
      getError: (data: any) => !data.error ? null : data.error || 'Unknown'
    }
  }
}).debug(!Server.isProduction)
Mqtt({
    key: 'connection1',
    url: 'mqtt://192.168.0.56',
    topic: {
      'LOG_RECEIVER': {
        des: 'Listen queue message for Log realtime',
        getListenerName: (_data: any) => `${AppConfig.mqtt.queue.LOG_RECEIVER}`,
        getError: (data: any) => !data.error ? null : data.error || 'Unknown'
      }
    }
})

Use in service file

import { MQTT, Mqtt, MqttListener } from 'hinos-mqtt'

@MqttListener({
  des: 'Listen from queue mqtt',
  topic: 'LOG_RECEIVER'
  listenerName: AppConfig.mqtt.queue.API_RECEIVER,
  onHandler(err, data) {
    console.log(err, data)
  }
})
export class ChartService {
    @MQTT()
    static mqtt: Mqtt;
    
    @MQTT('connection1')
    static mqtt1: Mqtt;
    
    static async sendToMqtt(data: any = {}) {
        ChartService.mqtt.send('topic', data);
    }
}