This is very tiny MQTT broker which is not managing any objects/states in iobroker but offers a central MQTT broker instance to publish an subscribe topics as MQTT client. Very helpful to let several devices to talk with one broker and interact on iobroker with a MQTT client javascript.
This adapter uses Sentry libraries to automatically report exceptions and code errors to the developers. For more details and for information how to disable the error reporting see Sentry-Plugin Documentation!
- Node.js 18 or higher
- ioBroker host (js-controller) 5.0 or higher
A MQTT client could look like
const mqtt = require('mqtt');
const protocol = 'mqtt';
const host = 'localhost';
const portClient = 1884;
const clientId = `iobroker_mqtt_client_` + Math.floor(Math.random() * 100000 + 100000);
const connectUrl = `${protocol}://${host}:${portClient}`;
const client = mqtt.connect(connectUrl, {
clientId,
clean: true,
connectTimeout: 4000,
reconnectPeriod: 10000
})
const topics = ['topic1', 'topic2', 'topic3];
client.on('connect', () => {
client.subscribe(topics, () => {
console.log(`MQTT client says: Connected and subscribe to topics '${topics}'`)
})
})
client.on('message', (topic: string, payload) => {
payload = payload.toString();
// deal as you need with topics and payload here
// 'switch' could be helpful
switch (topic) {
case 'topic1':
//your code
break;
case 'topic2':
//your code
break;
})
For publishing message I use one ioBroker state listening for any changes and pushing it to the broker. The state expects a JSON with 'topic' and 'message'.
on({ id: stateMqttIn, change: 'any' }, function (obj) {
let input: any = obj.state.val;
let topic: string = input.topic;
let message: string = String(input.message);
if (topic && message) client.publish(topic, message);
else log(`MQTT publish not possible with topic '${topic}' and message '${message}'`,'warn');
});
IMPORTANT! If you create your own MQTT client in an ioBroker javascript, do not forget to close the client in the scipt by using
onStop(function (callback) {
log('MQTT Client will be closed...');
client.end(() => {
if (callback) {
callback();
log('MQTT Client closed');
}
})
}, 2000 /*ms*/);
- (HGlab01) Improve port scan for available ports
- (HGlab01) Improve UI config
- (HGlab01) Bump json-explorer to 0.1.16
- (HGlab01) Bump aedes to 0.51.3
- (HGlab01) Bump json-explorer to 0.1.15
- (HGlab01) Bump aedes to 0.51.0
- (HGlab01) first minor release
- (HGlab01) Node.js 18.0 or higher
- (HGlab01) ioBroker host (js-controller) 5.0 or higher
- (HGlab01) Bump json-explorer to 0.1.14
- (HGlab01) add logs for "address already in use"-issue
- (HGlab01) Improve error handling
- (HGlab01) Verify if port is used from other process
- (HGlab01) add sentry support
- (HGlab01) first release
MIT License
Copyright (c) 2024 HGlab01 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.