Created
February 6, 2014 03:29
-
-
Save matbor/8837988 to your computer and use it in GitHub Desktop.
Simply connects to a mqtt broker that is running websockets, subscribes to the topics and displays the messages in the console. Requires mqttws31.js to work. Get it from http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.javascript.git/tree/src
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <title>Test subcriber</title> | |
| <head> | |
| <script src="vendor/mqttws31.js" type="text/javascript"></script> | |
| <script type="text/javascript"> | |
| //mqtt broker setup | |
| //Create a new Client object with your broker's hostname, port and your own clientId | |
| //var client = new Messaging.Client(hostname, port, clientid); | |
| var client = new Messaging.Client('broker.mqttdashboard.com', 8000, 'myclientid1234'); | |
| client.onMessageArrived = function (message) { | |
| console.log(message.destinationName + ' | ' + message.payloadString); | |
| }; | |
| var options = { | |
| //connection attempt timeout in seconds | |
| timeout: 3, | |
| //Gets Called if the connection has successfully been established | |
| onSuccess: function () { | |
| console.log("Connected"); | |
| //add the topics you want to subscribe to | |
| client.subscribe('/abc/123', {qos: 2}); | |
| client.subscribe('/abcd/1234', {qos: 2}); | |
| client.subscribe('/abcde/12345', {qos: 2}); | |
| client.subscribe('/abcdef/123456', {qos:2}); | |
| }, | |
| //Gets Called if the connection could not be established | |
| onFailure: function (message) { | |
| alert("Connection failed: " + message.errorMessage); | |
| } | |
| }; | |
| //this is what is loaded when the body of the html has called | |
| function init() { | |
| // Connect to MQTT broker | |
| client.connect(options); | |
| } | |
| </script> | |
| </head> | |
| <body onload="init();"> | |
| testing using the pahoo mqttws31.js file | |
| <p> | |
| NOTE: all output going to console | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment