Skip to content

Instantly share code, notes, and snippets.

@KEVINALBO13
Created September 14, 2018 02:10
Show Gist options
  • Save KEVINALBO13/2e33bfe334428e63d9c663a7e20f3e7f to your computer and use it in GitHub Desktop.
Save KEVINALBO13/2e33bfe334428e63d9c663a7e20f3e7f to your computer and use it in GitHub Desktop.
MQTT.JS
// Create a client instance
var client = new Paho.MQTT.Client('test.mosquitto.org', 1883, "clientId");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "World";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment