@core-services/mqtt
v1.0.0
Published
This library provides a simple wrapper around the MQTT client with support for:
Readme
MQTT Service – Usage Guide
This library provides a simple wrapper around the MQTT client with support for:
Easy connection setup
Topic subscription
Wildcard topic handlers (+ and #)
Automatic pattern → regex conversion
Installation
npm install @core-services/mqtt
Import and Initialize
import { MqttService } from "./src/mqttServices.js";
const brokerUrl = ${brokerUrl};
const mqttInstance = MqttService.getInstance(
clientID,
brokerUrl,
username,
password,
(statusCode, detail, message) => {
console.info(statusCode, detail, message);
}
);
Connect and Subscribe
mqttInstance.connect([topics]);
Example:
mqttInstance.connect(["vehicle/data" , "robots/+/hmi/ack"]);
Register Handlers
Handler for exact topic: mqttInstance.handler("vehicle/data", (message) => { console.info("message is", message); });
Handler for wildcard topic:
mqttInstance.handler("robots/+/hmi/ack", (message, topic) => { console.info("message is ", message, "and topic is", topic); });
