cordova-plugin-mqtt is plugin for building MQTT client for multiple platforms in Apache Cordova. Currently Android platform is present and next support is planned for iOS & Windows Phone.
5.x (CLI) 4.x (Cordova Android)
0.2.7 (Fixed some bugs. Check out the sample app code to build a proper MQTT client.)
Install plugins via plugin repository or GitHub
$ cordova plugin add cordova-plugin-mqtt
$ cordova plugin add https://github.com/arcoirislabs/cordova-plugin-mqtt.git
- New sample application is added.
- Fixed the subscribe callback issue.
- Some minor workarounds.
[UPDATE]
We have written a tutorial for this plugin over here. Kindly check out before you start developing. Cheers
Default listeners you can program anywhere for following events
- connected
- disconnected
- failure (connection)
- subscribed
- not subscribed
- published
- not published
For example you can configure the event in this way
document.addEventListener("connected",function(e){
console.log(e.type)
},false)
To connect to a broker. This plugin doesn't supports mqtt:// protocol. Use tcp:// instead.
cordova.plugins.CordovaMqTTPlugin.connect({
url:"tcp://test.mosquitto.org", //a public broker used for testing purposes only. Try using a self hosted broker for production.
port:1883,
clientId:"YOUR_USER_ID_LESS_THAN_24_CHARS",
connectionTimeout:3000,
willTopicConfig:{
qos:0,
retain:true,
topic:"<will topic>",
payload:"<will topic message>"
},
username:"uname",
password:'pass',
keepAlive:60,
success:function(s){
console.log("connect success");
},
error:function(e){
console.log("connect error");
},
onConnectionLost:function (){
console.log("disconnect");
}
})
To publish to a channel. You can use this function.
cordova.plugins.CordovaMqTTPlugin.publish({
topic:"sampletopic",
payload:"hello from the plugin",
qos:0,
retain:false
success:function(s){
},
error:function(e){
}
})
In order to debug the publish call you can either go for callbacks in the function or events. Once published the function will call the "published" event & the success callback else the function will call both "not published" event & error callback.
To subscribe to a channel. You can use this function.
cordova.plugins.CordovaMqTTPlugin.subscribe({
topic:"sampletopic",
qos:0,
success:function(s){
},
error:function(e){
}
})
The success callback can notify you once you are successfully subscribed, so it will be called only once. The onPublish method is deprecated. If you want to read the payload, you can listen to the event by the name of the topic. For example if you have subscribed to the topic called "sampletopic". You can read the payload in this way.
document.addEventListener("sampletopic",function(e){
console.log(e.payload)
},false)
To unsubscribe to a channel. You can use this function.
cordova.plugins.CordovaMqTTPlugin.unsubscribe({
topic:"sampletopic",
success:function(s){
},
error:function(e){
}
})
This function will also fire the unsubscribe event which yiu can listen to using the document.addEventListener function. Also the event listener for the topic is removed automatically once the client successfully unsubscibes.
To disconnect yourself from a server, use following function
cordova.plugins.CordovaMqTTPlugin.disconnect({
success:function(s){
},
error:function(e){
}
})
- Plan support for new platforms (iOS, Windows Phone)
- Add background service support in Android version
MIT