Adonis-mqtt is a provider to integrate MQTT in your app. This is useful when developing an IOT application, or need a simple message broker.
- MQTT Connection using mqtt.js
- Easy integration with MqttListener classes
- Included CLI generator for MqttListener classes
These are the features that I still want to developing in the near future.
- Check SSL support (currently untested)
- Improve tests and documentation
npm i --save adonis-mqtt
To use adonis-mqtt, add the following to your providers:
// Add the mqtt provider
const providers = [
// ...
'adonis-mqtt/providers/MqttProvider',
]
// ...
// Add the command provider
const aceProviders = [
// ...
'adonis-mqtt/providers/CommandsProvider',
]
Then we'll have to use
Mqtt somewhere so it gets initialized. I prefer it in the app.events.js
file, since all event-related stuff is defined there.
This is only needed if you do not use it anywhere else (for example when sending messages).
const Event = use('Event');
const Mqtt = use('Mqtt');
// Listen to some Events of the library
Event.on('MQTT:Connected', 'Message.connected')
Event.on('MQTT:Disconnected', 'Message.disconnected')
Lastly we should add some configuration to the .env
file so MQTT knows where and how to connect
MQTT_HOST=yourmqtthost.com
MQTT_PORT=10444
MQTT_USERNAME=username123
MQTT_PASSWORD=password123#
MQTT_CLIENTID=abcabc_11
Now adonis-mqtt is ready for use.
Sending messages through your MQTT server is very easy.
const Mqtt = use('Mqtt');
class SomeController {
async index() {
await Mqtt.sendMessage('mytopic', 'My Message', {qos: 1})
}
}
You can define multiple MqttListeners, which the provider will automatically pick up on boot.
You can generate a listener by using the adonis cli:
adonis make:mqtt-listener Test
This command will generate a listener class called TestMqttListener.
MqttListeners should are defined in the app/MqttListeners
folder and should have the following base content:
const MqttListener = use('MqttListener')
class MockListener extends MqttListener {
/*
* Subscription string. Uses the MQTT wildcard format.
*/
get subscription () {
return 'my/+/example/mqtt/string/#'
}
/**
* Message handler is passed the String data of the message and the matched wildcard values
*/
async handleMessage (message, wildcardMatches) {
}
}
module.exports = MockListener
You are free to contribute, make pull requests and create issues.
I will work on this project in my free time and will be using it to build my own home automation system.
The code of adonis-scheduler was a great example in many ways. This helped me tremendously.