Skip to content

Instantly share code, notes, and snippets.

@matbor
Created February 6, 2014 03:29
Show Gist options
  • Select an option

  • Save matbor/8837988 to your computer and use it in GitHub Desktop.

Select an option

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
<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