0

I am using an mqtt client with EMQ X broker (that is running as a service in my local linux machine) and tested the pub sub mechanism but I want to use broker event function (connect, disconnect, publish and subscribe) for adding a custom logic into broker events. Kindly guide me how can i achieve this in emq X broker ?

1 Answers1

0

Build your own logic using EMQ X REST API

https://github.com/wivwiv/egg-iot-with-mqtt

It is recommended to use the MQTT.js:

https://www.npmjs.com/package/mqtt#connect

const mqtt = require('mqtt')

// const url = 'mqtt://localhost:1883'
const url = 'mqtt://tools.emqx.io:1883'

const clinet = mqtt.connect(url, {
  clientId: 'emqx_client_id',
  username: '',
  password: '',
})

client.on('connect', () => {
  client.subscribe('presence', (err) => {
    if (!err) {
      setInterval(() => {
        client.publish('presence', 'Hello EMQ X')
      }, 1000)
    }
  })
})

client.on('message', (topic, message) => {
  // message is Buffer
  console.log('received from', topic, message.toString())
})

wivwiv
  • 255
  • 1
  • 3