1.0.1 • Published 1 year ago

fastify-async-mqtt v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

fastify-async-mqtt

Fastify Async Mqtt is wrapper over async-mqtt with TypeScript support.

Install

npm i fastify-async-mqtt

Usage

Add it to your project with register and you are done!

import fastify from 'fastify'
import { fastifyMQTT } from './mqtt-module'

const server = fastify()

// register plugin 
server.register(fastifyMQTT, { url: 'mqtt://localhost:1883' })

server.register(async function (instance) { 
    if (!instance.mqttClient) return
    await instance.mqttClient.publish("hello/world", "Hi Mosquitto");
  })

server.listen({ port: 3000, host: '0.0.0.0' }, err => {
  if (err) throw err
})

or usage on request

// another type of register
server.register(fastifyMQTT, { 
        opts: {host: 'mosquitto', port: 1883, protocol: 'mqtt'}, 
        allowRetries: false 
    })

server.get('/ping/mqtt', async function (req, reply) {
    await this.mqttClient.publish("hello/world", "Hi Mosquitto");
    reply.send({mqtt: 'pinged'})
})

License

Licensed under MIT.

1.0.1

1 year ago

1.0.0

1 year ago