├── index.js ├── src ├── index.html ├── test.js └── angular-paho.js ├── package.json ├── README.md ├── webserver.js ├── bower.json ├── LICENSE └── dist └── angular-paho.js /index.js: -------------------------------------------------------------------------------- 1 | require('angular'); 2 | require('./angular-paho') 3 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 |
7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-paho", 3 | "version": "1.0.0", 4 | "description": "angular directive for paho", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Bipol Alam ", 10 | "license": "MIT", 11 | "devDependencies": { 12 | "express": "^4.13.4" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # angular-paho 2 | simple angularjs wrapper for paho mqtt client 3 | 4 | This is a very simple wrapper for paho - just wrap the client functions and allow a user to initialize a single connection to a server. 5 | The factory object can then be passed around to other controllers in order to send/recieve messages to/from a broker. 6 | 7 | ## Quick start 8 | * Check the test.js file for an example 9 | -------------------------------------------------------------------------------- /webserver.js: -------------------------------------------------------------------------------- 1 | //set-up 2 | var express = require('express'); 3 | 4 | var app = express(); 5 | 6 | app.use(express.static(__dirname)); 7 | app.use(express.static(__dirname + '/src/')); 8 | app.use(express.static(__dirname + '/node_modules/')); 9 | app.use(express.static(__dirname + '/bower_components/')); 10 | 11 | app.get('/', function(req, res) { 12 | res.sendFile('./index.html'); // load the single view file (angular will handle the page changes on the front-end) 13 | }); 14 | 15 | app.listen('8080'); 16 | console.log("App listening on port 8080"); 17 | -------------------------------------------------------------------------------- /src/test.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | angular.module('app', ['angularPaho']); 3 | })(); 4 | 5 | (function() { 6 | angular.module('app').controller('test', [ '$scope', 'MqttClient', function($scope, MqttClient) { 7 | 8 | var ip = "192.168.10.241"; 9 | var port = "9001"; 10 | var id = "test"; 11 | 12 | MqttClient.init(ip, port, id); 13 | MqttClient.connect({onSuccess: successCallback}); 14 | 15 | function successCallback() { 16 | MqttClient.subscribe('/World'); 17 | message = new Paho.MQTT.Message("Hello"); 18 | message.destinationName = "/World"; 19 | MqttClient.send(message); 20 | } 21 | }]); 22 | })(); 23 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-paho", 3 | "authors": [ 4 | "Bipol Alam " 5 | ], 6 | "description": "paho directive for angular", 7 | "main": "dist/angular-paho.js", 8 | "moduleType": [], 9 | "keywords": [ 10 | "angular", 11 | "paho", 12 | "mqtt", 13 | "directive" 14 | ], 15 | "license": "MIT", 16 | "homepage": "", 17 | "ignore": [ 18 | "**/.*", 19 | "node_modules", 20 | "LICENSE", 21 | "webserver.js", 22 | "index.js", 23 | "package.json", 24 | "README.md", 25 | "src", 26 | "bower_components", 27 | "test", 28 | "tests" 29 | ], 30 | "dependencies": { 31 | "paho-mqtt-js": "^1.0.1" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Bipol Alam 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. 22 | -------------------------------------------------------------------------------- /dist/angular-paho.js: -------------------------------------------------------------------------------- 1 | /* 2 | author: Bipol Alam, 3 | description: a really simple wrapper for the paho mqtt service 4 | */ 5 | 6 | (function() { 7 | angular.module('angularPaho', []); 8 | })(); 9 | 10 | (function() { 11 | angular.module('angularPaho').factory('MqttClient', [function() { 12 | 13 | // so we can use the member attributes inside our functions 14 | var client = {}; 15 | 16 | // initialize attributes 17 | client._location = ""; 18 | client._port = ""; 19 | client._id = ""; 20 | client._client = null; 21 | client._isConnected = false; 22 | 23 | // member functions 24 | client.init = init; 25 | client.connect = connect; 26 | client.disconnect = disconnect; 27 | client.send = send; 28 | client.startTrace = startTrace; 29 | client.stopTrace = stopTrace; 30 | client.subscribe = subscribe; 31 | client.unsubscribe = unsubscribe; 32 | 33 | return client; 34 | 35 | // onConnectionLost callback 36 | 37 | function _call(cb, args) { 38 | if (client._client) { 39 | cb.apply(this, args); 40 | } else { 41 | console.log('Angular-Paho: Client must be initialized first. Call init() function.'); 42 | } 43 | } 44 | 45 | function onConnectionLost(resp) { 46 | console.log("Angular-Paho: Connection lost on ", client._id, ", error code: ", resp); 47 | client._isConnected = false; 48 | } 49 | 50 | // connects to the MQTT Server 51 | function connect(options) { 52 | _call(_connect, [options]); 53 | } 54 | 55 | function _connect(options) { 56 | client._client.connect(options); 57 | client._isConnected = client._client.isConnected(); 58 | } 59 | 60 | function disconnect() { 61 | _call(_disconnect); 62 | } 63 | 64 | function _disconnect() { 65 | client._client.disconnect(); 66 | client._isConnected = false; 67 | } 68 | 69 | function init(location, port, id) { 70 | // initialize attributes 71 | client._location = location; 72 | client._port = port; 73 | client._id = id; 74 | 75 | // create the client and callbacks 76 | client._client = new Paho.MQTT.Client(client._location, Number(client._port), client._id); 77 | client._client.onConnectionLost = onConnectionLost; 78 | client._client.onMessageArrived = onMessageArrived; 79 | } 80 | 81 | function onMessageArrived(message) { 82 | console.log("onMessageArrived:"+message.payloadString); 83 | } 84 | 85 | function send(message) { 86 | _call(_send, [message]); 87 | } 88 | 89 | function _send(message) { 90 | client._client.send(message); 91 | } 92 | 93 | function startTrace() { 94 | _call(_startTrace); 95 | } 96 | 97 | function _startTrace() { 98 | client._client.startTrace(); 99 | } 100 | 101 | function stopTrace() { 102 | _call(_stopTrace); 103 | } 104 | 105 | function _stopTrace() { 106 | client._client.stopTrace(); 107 | } 108 | 109 | function subscribe(filter, options) { 110 | _call(_subscribe, [filter, options]); 111 | } 112 | 113 | function _subscribe(filter, options) { 114 | client._client.subscribe(filter, options); 115 | } 116 | 117 | function unsubscribe(filter, options) { 118 | _call(_unsubscribe, [filter, options]); 119 | } 120 | 121 | function _unsubscribe(filter, options) { 122 | client._client.unsubscribe(filter, options); 123 | } 124 | 125 | }]); 126 | })(); 127 | -------------------------------------------------------------------------------- /src/angular-paho.js: -------------------------------------------------------------------------------- 1 | /* 2 | author: Bipol Alam, 3 | description: a really simple wrapper for the paho mqtt service 4 | */ 5 | 6 | (function() { 7 | angular.module('angularPaho', []); 8 | })(); 9 | 10 | (function() { 11 | angular.module('angularPaho').factory('MqttClient', [function() { 12 | 13 | // so we can use the member attributes inside our functions 14 | var client = {}; 15 | 16 | // initialize attributes 17 | client._location = ""; 18 | client._port = ""; 19 | client._id = ""; 20 | client._client = null; 21 | client._isConnected = false; 22 | 23 | // member functions 24 | client.init = init; 25 | client.connect = connect; 26 | client.disconnect = disconnect; 27 | client.send = send; 28 | client.startTrace = startTrace; 29 | client.stopTrace = stopTrace; 30 | client.subscribe = subscribe; 31 | client.unsubscribe = unsubscribe; 32 | 33 | return client; 34 | 35 | // onConnectionLost callback 36 | 37 | function _call(cb, args) { 38 | if (client._client) { 39 | cb.apply(this, args); 40 | } else { 41 | console.log('Angular-Paho: Client must be initialized first. Call init() function.'); 42 | } 43 | } 44 | 45 | function onConnectionLost(resp) { 46 | console.log("Angular-Paho: Connection lost on ", client._id, ", error code: ", resp); 47 | client._isConnected = false; 48 | } 49 | 50 | // connects to the MQTT Server 51 | function connect(options) { 52 | _call(_connect, [options]); 53 | } 54 | 55 | function _connect(options) { 56 | client._client.connect(options); 57 | client._isConnected = client._client.isConnected(); 58 | } 59 | 60 | function disconnect() { 61 | _call(_disconnect); 62 | } 63 | 64 | function _disconnect() { 65 | client._client.disconnect(); 66 | client._isConnected = false; 67 | } 68 | 69 | function init(location, port, id) { 70 | // initialize attributes 71 | client._location = location; 72 | client._port = port; 73 | client._id = id; 74 | 75 | // create the client and callbacks 76 | client._client = new Paho.MQTT.Client(client._location, Number(client._port), client._id); 77 | client._client.onConnectionLost = onConnectionLost; 78 | client._client.onMessageArrived = onMessageArrived; 79 | } 80 | 81 | function onMessageArrived(message) { 82 | console.log("onMessageArrived:"+message.payloadString); 83 | } 84 | 85 | function send(message) { 86 | _call(_send, [message]); 87 | } 88 | 89 | function _send(message) { 90 | client._client.send(message); 91 | } 92 | 93 | function startTrace() { 94 | _call(_startTrace); 95 | } 96 | 97 | function _startTrace() { 98 | client._client.startTrace(); 99 | } 100 | 101 | function stopTrace() { 102 | _call(_stopTrace); 103 | } 104 | 105 | function _stopTrace() { 106 | client._client.stopTrace(); 107 | } 108 | 109 | function subscribe(filter, options) { 110 | _call(_subscribe, [filter, options]); 111 | } 112 | 113 | function _subscribe(filter, options) { 114 | client._client.subscribe(filter, options); 115 | } 116 | 117 | function unsubscribe(filter, options) { 118 | _call(_unsubscribe, [filter, options]); 119 | } 120 | 121 | function _unsubscribe(filter, options) { 122 | client._client.unsubscribe(filter, options); 123 | } 124 | 125 | }]); 126 | })(); 127 | --------------------------------------------------------------------------------