├── .travis.yml ├── package.json ├── src ├── ios │ ├── CordovaMQTTPlugin.m │ ├── MQTTClientPersistence.h │ └── MQTTAsync.h └── android │ └── CordovaMqTTPlugin.java ├── LICENSE ├── plugin.xml ├── sampleapp ├── index.html └── js │ └── index.js ├── README.md └── www ├── cordova-plugin-mqtt.js └── MQTTEmitter.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "4.2" 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-mqtt", 3 | "version": "0.3.8", 4 | "description": "Cordova plugin for MQTT (Message Queuing Telemetry Transport) protocol. Supports 3.x protocol. More platforms to be added in coming future.", 5 | "cordova": { 6 | "id": "cordova-plugin-mqtt", 7 | "platforms": [ 8 | "android" 9 | ] 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/arcoirislabs/mqtt-cordova.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/arcoirislabs/mqtt-cordova/issues" 17 | }, 18 | "keywords": [ 19 | "cordova", 20 | "ecosystem:cordova", 21 | "mqtt", 22 | "arcoirislabs" 23 | ], 24 | "author": { 25 | "name": "Arcoiris Labs", 26 | "email": "amey@arcoirislabs.com" 27 | }, 28 | "license": "MIT" 29 | } 30 | -------------------------------------------------------------------------------- /src/ios/CordovaMQTTPlugin.m: -------------------------------------------------------------------------------- 1 | /********* CDVCordovaMQTTPlugin.m Cordova Plugin Implementation *******/ 2 | 3 | #import 4 | #import "MQTTAsync.h" 5 | @interface CDVCordovaMQTTPlugin : CDVPlugin { 6 | // Member variables go here. 7 | } 8 | 9 | - (void)coolMethod:(CDVInvokedUrlCommand*)command; 10 | @end 11 | 12 | @implementation CDVCordovaMQTTPlugin 13 | 14 | - (void)coolMethod:(CDVInvokedUrlCommand*)command 15 | { 16 | CDVPluginResult* pluginResult = nil; 17 | NSString* echo = [command.arguments objectAtIndex:0]; 18 | 19 | if (echo != nil && [echo length] > 0) { 20 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo]; 21 | } else { 22 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; 23 | } 24 | 25 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 26 | } 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Arcoiris Labs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | CordovaMqTTPlugin 4 | The new MqTT plugin for Cordova 5 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /sampleapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Mqtt Test 13 | 14 | 15 |
16 |

Connect

17 |

P.S:- "mqtt://" protocol is not supported by this plugin. Instead use "tcp://" which works with any broker.

18 |
19 |
20 |
21 | 22 |
23 |
24 |

Subscribe

25 |
26 | 27 |
28 |
29 |

Publish

30 |
31 | 32 | 33 |
34 |
35 |

Activity Log

36 |
37 |
38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /sampleapp/js/index.js: -------------------------------------------------------------------------------- 1 | 2 | var connect = false; 3 | var app = { 4 | // Application Constructor 5 | initialize: function() { 6 | this.bindEvents(); 7 | }, 8 | // Bind Event Listeners 9 | // 10 | // Bind any events that are required on startup. Common events are: 11 | // 'load', 'deviceready', 'offline', and 'online'. 12 | bindEvents: function() { 13 | document.addEventListener('deviceready', this.onDeviceReady, false); 14 | }, 15 | // deviceready Event Handler 16 | // 17 | // The scope of 'this' is the event. In order to call the 'receivedEvent' 18 | // function, we must explicitly call 'app.receivedEvent(...);' 19 | onDeviceReady: function() { 20 | app.receivedEvent('deviceready'); 21 | }, 22 | // Update DOM on a Received Event 23 | receivedEvent: function(id) { 24 | document.getElementById("connect").addEventListener('touchend',function(ev){ 25 | cordova.plugins.CordovaMqTTPlugin.connect({ 26 | url:document.getElementById("url").value, //a public broker used for testing purposes only. Try using a self hosted broker for production. 27 | port:document.getElementById("port").value, 28 | clientId:document.getElementById("clientId").value, 29 | success:function(s){ 30 | connect = true; 31 | console.log(JSON.stringify(s)); 32 | document.getElementById("connect").style.display = "none"; 33 | document.getElementById("disconnect").style.display = "block"; 34 | document.getElementById("activity").innerHTML += "--> Success: you are connected to, "+document.getElementById("url").value+":"+document.getElementById("port").value+"
" 35 | }, 36 | error:function(e){ 37 | connect = false; 38 | document.getElementById("activity").innerHTML += "--> Error: something is wrong,\n "+JSON.stringify(e)+"
"; 39 | document.getElementById("connect").style.display = "block"; 40 | document.getElementById("disconnect").style.display = "none"; 41 | alert("err!! something is wrong. check the console") 42 | console.log(e); 43 | }, 44 | onConnectionLost:function (){ 45 | connect = false; 46 | document.getElementById("activity").innerHTML += "--> You got disconnected"; 47 | document.getElementById("connect").style.display = "block"; 48 | document.getElementById("disconnect").style.display = "none"; 49 | } 50 | }) 51 | }); 52 | document.getElementById("disconnect").addEventListener('touchend',function(e){ 53 | document.getElementById("connect").style.display = "block"; 54 | document.getElementById("disconnect").style.display = "none"; 55 | cordova.plugins.CordovaMqTTPlugin.disconnect({ 56 | success:function(s){ 57 | connect = false; 58 | document.getElementById("connect").style.display = "block"; 59 | document.getElementById("disconnect").style.display = "none"; 60 | document.getElementById("activity").innerHTML += "--> Success: you are now disconnected"+"
" 61 | }, 62 | error:function(e){ 63 | document.getElementById("activity").innerHTML += "--> Error: something is wrong, "+e+"
"; 64 | document.getElementById("connect").style.display = "none"; 65 | document.getElementById("disconnect").style.display = "block"; 66 | //alert("err!! something is wrong. check the console") 67 | console.log(e); 68 | } 69 | }); 70 | }); 71 | document.getElementById("subscribe").addEventListener('touchend',function(ev){ 72 | if (!connect) { 73 | alert("First establish connection then try to subscribe"); 74 | } else { 75 | cordova.plugins.CordovaMqTTPlugin.subscribe({ 76 | topic:document.getElementById("topic_sub").value, 77 | qos:0, 78 | success:function(s){ 79 | document.getElementById("subscribe").style.display = "none"; 80 | document.getElementById("unsubscribe").style.display = "block"; 81 | document.getElementById("activity").innerHTML += "--> Success: you are subscribed to the topic, "+document.getElementById("topic_sub").value+"
" 82 | //get your payload here 83 | //Deprecated method 84 | document.addEventListener(document.getElementById("topic_sub").value,function (e) { 85 | e.preventDefault(); 86 | 87 | document.getElementById("activity").innerHTML += "--> Payload for"+e.topic+" topic: "+JSON.stringify(e.payload)+"
" 88 | }); 89 | 90 | cordova.plugins.CordovaMqTTPlugin.listen(document.getElementById("topic_sub").value,function (payload,params,topic,topic_pattern) { 91 | //params will be an empty object if topic pattern is NOT used. 92 | document.getElementById("activity").innerHTML += "--> Payload for"+topic+" topic: "+JSON.stringify(payload)+"
" 93 | }) 94 | }, 95 | error:function(e){ 96 | document.getElementById("activity").innerHTML += "--> Error: something is wrong when subscribing to this topic, "+e+"
"; 97 | document.getElementById("subscribe").style.display = "block"; 98 | document.getElementById("unsubscribe").style.display = "none"; 99 | //alert("err!! something is wrong. check the console") 100 | console.log(e); 101 | } 102 | }); 103 | } 104 | }); 105 | document.getElementById("unsubscribe").addEventListener('touchend',function(ev){ 106 | cordova.plugins.CordovaMqTTPlugin.unsubscribe({ 107 | topic:document.getElementById("topic_sub").value, 108 | success:function(s){ 109 | document.removeEventListener(document.getElementById("topic_sub").value); 110 | document.getElementById("unsubscribe").style.display = "none"; 111 | document.getElementById("subscribe").style.display = "block"; 112 | document.getElementById("activity").innerHTML += "--> Success: you are unsubscribed to the topic, "+document.getElementById("topic_sub").value+"
" 113 | document.getElementById("topic_sub").value = ""; 114 | }, 115 | error:function(e){ 116 | document.getElementById("activity").innerHTML += "--> Error: something is wrong, "+e+"
"; 117 | document.getElementById("subscribe").style.display = "block"; 118 | document.getElementById("unsubscribe").style.display = "none"; 119 | //alert("err!! something is wrong. check the console") 120 | console.log(e); 121 | } 122 | }); 123 | }); 124 | document.getElementById("publish").addEventListener('touchend',function(ev){ 125 | if (!connect) { 126 | alert("First establish connection then try to publish") 127 | } else { 128 | cordova.plugins.CordovaMqTTPlugin.publish({ 129 | topic:document.getElementById("topic_pub").value, 130 | payload:document.getElementById("payload").value, 131 | qos:0, 132 | retain:false, 133 | success:function(s){ 134 | document.getElementById("activity").innerHTML += "--> Success: you have published to the topic, "+document.getElementById("topic_sub").value+"
"; 135 | }, 136 | error:function(e){ 137 | document.getElementById("activity").innerHTML += "--> Error: something is wrong, "+e+"
"; 138 | //alert("err!! something is wrong. check the console") 139 | console.log(e); 140 | } 141 | }); 142 | } 143 | }); 144 | console.log('Received Event: ' + id); 145 | }, 146 | append:function (id,s) { 147 | // it is just a string append function. Nothing to do with the MQTT functions 148 | var node = document.createElement("p"); // Create a
  • node 149 | var textnode = document.createTextNode(s); // Create a text node 150 | node.appendChild(textnode); // Append the text to
  • 151 | document.getElementById(id).appendChild(node); // Append
  • to