@sladdky/ah-mqttrouter
v0.0.3
Published
MQTT router for mqttjs/mqtt library.
Downloads
1
Readme
AH-MQTTROUTER
Naive implementation of MqttRouter for MQTT.js library. Route handling similar to Express's router.
Table of contents
- installation
- usage
- API
- MqttRouteCallback (fire-and-forget)
- MqttRouteCallback (request-response) + MqttRequestor
- middleware
- re-routing
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.
- mqtt message fired from some client:
topic: "my-mqtt-topic"
message: "This is my message."
- 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.
- JSON must be in this form:
{
responseTopic: "random-topic-the-requester-must-subscribe-before-sending",
message: "This is my message"
}
- 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"}'
- 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