Ember async service for connecting to mqtt broker through mqttjs library.
- Ember.js v3.12 or above
- Ember CLI v2.13 or above
- Node.js v10 or above
ember install ember-mqttjs
Be sure that in your ember-cli-build.js
you have the following rows to import mqtt npm package:
app.import('node_modules/mqtt/dist/mqtt.min.js',{
using: [
{ transformation: 'amd', as: 'mqttjs' }
]
});
You have to import this service in your route or controller or component or service js class:
import { inject as service } from '@ember/service';
...
@service mqtt;
Connect to the mqtt host and register a listener to the mqtt-message
event:
this.mqtt.connect('wss://test.mosquitto.org:8081').then( () => {
// Do stuff on connection established
});
this.mqtt.on('mqtt-message', (sTopic, sMessage) => {
// Do stuff with topic and message parameters
});
this.mqtt.subscribe('presence').then( (oGranted)=>{
// Do stuff after succesfully subscription to mqttTopic
});
oGranted
is an array of {topic, qos}
where:
- topic: is a subscribed to topic
- qos: is the granted QoS level on it
this.mqtt.publish('presence', 'Hello').then( () => {
// Do stuff after successfully published message
});
this.mqtt.unsubscribe('presence').then( (oGranted)=>{
// Do stuff after succesfully unsubscription to mqttTopic
});
New mqtt message received.
this.mqtt.on('mqtt-message', (sTopic, sMessage) => {
// Do stuff with topic and message parameters
});
Connected event.
Disconnected event.
Error event.
Fired when mqtt starts a reconnection
Closed connection event.
Fired when mqtt goes offline.
See the Contributing guide for details.
This project is licensed under the MIT License.