@sladdky/ah-mqttrouter
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

AH-MQTTROUTER

Naive implementation of MqttRouter for MQTT.js library. Route handling similar to Express's router.


Table of contents


Instalation

npm install @sladdky/ah-mqttrouter --save

Usage

import { connect } from 'mqtt';
import { MqttRouter } from '@sladdky/ah-mqttrouter'

const onMyMqttTopic1 = (request, response, next) => {
  //...your route logic
}
const onMyMqttTopic2 = (request, response, next) => {
  //...your route logic
}

const mqttClient = connect(<mqtt://url>);
const mqttRouter = new MqttRouter(mqttClient, {
  routes: [
    {topic: 'my-mqtt-topic-1', callback: [onMyMqttTopic1]},
    {topic: 'my-mqtt-topic-2', callback: [onMyMqttTopic2]},
  ]
});

API

  • mqttRouter.subscribe(topic: string, callback: MqttRouteCallback | MqttRouteCallback[])
  • mqttRouter.unsubscribe(topic: string, callback: MqttRouteCallback | MqttRouteCallback[])
  • mqttRouter.publish(topic: string, message: string | Buffer, opts?: IClientPublishOptions, callback?: PacketCallback | undefined): MqttClient;

MqttRouteCallback (Fire-and-Forget)

That's MQTT primary usage. One MqttClient sends a message to certain topic and other MqttClients that are subscribed to that topic, receive the message. No strings attached, no response expected.

  1. mqtt message fired from some client:
topic: "my-mqtt-topic"
message: "This is my message."
  1. example route implementation:
const onMyMqttTopic = (request, response, next) => {
  const { topic, payload } = request

  saveToDB(`topic: ${topic}, payload: ${payload}`)

  response.send('Message received and I saved it to the database')
}

MqttRouteCallback (Request-Response)

If you need request-response type of communication it's possible. The message is always expected to be stringified JSON.

  1. JSON must be in this form:
{
  responseTopic: "random-topic-the-requester-must-subscribe-before-sending",
  message: "This is my message"
}
  1. mqtt message sent will look like this
topic: "my-mqtt-topic"
message: '{"responseTopic":"random-topic-the-requester-must-subscribe-before-sending","message":"This is my message"}'
  1. example route implementation:
const onMyMqttTopic = (request, response, next) => {
  const { topic, payload } = request

  saveToDB(`topic: ${topic}, payload: ${payload}`)
}

The MQTTclient that is requesting has to subscribe to 'responseTopic' and unsubscribe after the response comes back. You can either handle this situation youself or use asynchronous mqttRequestor.

Example:

...
import { createMqttRequestor } from '@sladdky/ah-mqttrouter/lib/utils'

const mqttRouter = new MqttRouter(mqttClient)
const mqttRequestor = createMqttRequestor(mqttRouter)
mqttRequestor.send('my-mqtt-topic', 'This is my message').then((response) => {
  console.log(response.payload)
})

Middleware (workaround)

Please add your middleware logic as a route callback.

Example:

const onAuthMiddleware = (request, response, next) => {
  if (!isLoggedIn) {
    //...not-allowed to proccess
  }

  next()
}
const onMyMqttTopic = (request, response) => {
  //...your route logic
}

mqttRouter.subscribe('my-mqtt-topic', [onAuthMiddleware, onMyMqttTopic])

Re-routing

Not implemented.



based on ts-npm-package-boilerplate

Readme

Keywords

Package Sidebar

Install

npm i @sladdky/ah-mqttrouter

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

14.7 kB

Total Files

11

Last publish

Collaborators

  • sladdky