block-messenger
Simplifies message exchange between pipeline blocks.
Factory Pattern simplifies replacement of Client.
MQTT Client is Implemented based on NPM mqtt package
To Create a MQTT Client using the BlockMessenger Factory
options = {type = 'mqtt', ...}
To receive messages, listen to event 'message' (topic, message)
To publish a message, messenger.publish(some_data, callback);
Usage example:
const BlockMessenger = require('block-messenger').BlockMessenger;
const bm = new BlockMessenger();
const options = {
type: "mqtt",
host: '127.0.0.1',
port: 1883,
username: 'username,
password: 'password,
subscribe_topics: ['some/topic'],
publish_topics: ['some/topic'],
use_shared_topics: true,
group_name: 'group'
};
const mqtt = bm.createMessenger(options);
mqtt.on('subscribed', (granted) => {
console.log(granted);
});
mqtt.on('message', (topic, message) => {
console.log(`new message in topic ${topic}:`, message.toString());
});
setInterval(() => {
mqtt.publish('test data', (err) => {
if (err) {
console.error(err);
}
else {
console.log('message sent');
}
});
}, 1000);
The MQTT Client also emits the following events:
-
connect
-
reconnect
-
close
-
offline
-
error
-
end
-
packetsend
-
packetreceive
-
message
-
subscribed
Run Tests
npm test
npm run-script test-cov
TO DO:
- Implement kafka-messenger.js (options.type = 'kafka')