Skip to content

Instantly share code, notes, and snippets.

@jpwsutton
Created November 30, 2016 11:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpwsutton/6427e38dd3d1db6ba11e48eb0712cba7 to your computer and use it in GitHub Desktop.
Save jpwsutton/6427e38dd3d1db6ba11e48eb0712cba7 to your computer and use it in GitHub Desktop.
Paho Javascript new Publish Example
<html>
<head>
<title>Eclipse Paho MQTT JavaScript Client Example</title>
<!-- Source Paho MQTT Client-->
<script src="../src/paho-mqtt.js"></script>
<!--<script src="../src/paho-mqtt.js"></script>-->
<!-- Utility Javascript -->
<script src="example.js"></script>
</body>
</html>
</head>
// Create a client instance
client = new Paho.MQTT.Client("iot.eclipse.org", Number(443), "/wss");
client.startTrace();
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect,
useSSL: true});
console.log("attempting to connect...")
// 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);
//console.log(client.getTraceLog());
//client.getTraceLog().forEach(function(line){
// console.log('Trace: ' + line)
//});
//newMessage = new Paho.MQTT.Message("Sent using synonyms!");
//newMessage.topic = "/World";
client.publish(message)
client.publish("/World", "Hello from a better publish call!", 1, false)
topicMessage = new Paho.MQTT.Message("This is a message where the topic is set by setTopic");
topicMessage.topic = "/World";
client.publish(topicMessage)
}
// 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);
}
@akashsinha261995
Copy link

Throwing error:
failed: Error in connection establishment: net::ERR_CONNECTION_RESET

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment