ibm bluemix - How to connect IBM Watson IOT using Paho MQTT javascript client? -


i trying connect ibm watson iot platform using paho mqtt javascript client mentioned in below example code.

    var client = new messaging.client("myoqgid.messaging.internetofthings.ibmcloud.com", 8883, "myclientid_" + parseint(math.random() * 100, 10));     //gets  called if websocket/mqtt connection gets disconnected reason   client.onconnectionlost = function (responseobject) {       //depending on scenario implement reconnect logic here       alert("connection lost: " + responseobject.errormessage);   };     //gets called whenever receive message subscriptions   client.onmessagearrived = function (message) {       //do push message received       $('#messages').append('<span>topic: ' + message.destinationname + '  | ' + message.payloadstring + '</span><br/>');   };     //connect options   var options = {       username: api-key here,       password: auth token here,       timeout: 3,       //gets called if connection has sucessfully been established       onsuccess: function () {           alert("connected");       },       //gets called if connection not established       onfailure: function (message) {           alert("connection failed: " + message.errormessage);       }   };     //creates new messaging.message object , sends hivemq mqtt broker   var publish = function (payload, topic, qos) {       //send message (also possible serialize json or protobuf or use string, no limitations)       var message = new messaging.message(payload);       message.destinationname = topic;       message.qos = qos;       client.send(message);   }

but not able connect. getting error: websocket connection 'ws://myorgidxyz.messaging.internetofthings.ibmcloud.com:8883/mqtt' failed: error during websocket handshake: net::err_connection_reset

please out there tried connecting ibm watson iot using paho mqtt client.

thank responses. based on responses have made changes in code.

<script type="text/javascript">    var clientid  = 'a:myorgid:'+math.random().tostring(16).substr(2, 8);  var client = new messaging.client("myoqgid.messaging.internetofthings.ibmcloud.com", 1883, clientid);     //gets  called if websocket/mqtt connection gets disconnected reason   client.onconnectionlost = function (responseobject) {       //depending on scenario implement reconnect logic here       alert("connection lost: " + responseobject.errormessage);   };     //gets called whenever receive message subscriptions   client.onmessagearrived = function (message) {       //do push message received       $('#messages').append('<span>topic: ' + message.destinationname + '  | ' + message.payloadstring + '</span><br/>');   };     //connect options   var options = {       username: api-key here,       password: auth token here,       timeout: 3,       //gets called if connection has sucessfully been established       onsuccess: function () {           alert("connected");    	        },       //gets called if connection not established       onfailure: function (message) {           alert("connection failed: " + message.errormessage);       }   };     //creates new messaging.message object , sends hivemq mqtt broker   var publish = function (payload, topic, qos) {       //send message (also possible serialize json or protobuf or use string, no limitations)       var message = new messaging.message(payload);       message.destinationname = topic;       message.qos = qos;       client.send(message);   }  client.connect(options);  </script>

you can see have made changes in clientid. ibm watson iot accept client ids in below format if not using watson iot libraries.

var clientid = 'a:orgid:'+randomstring;

if using ibm watson iot libraries client id can anything. implemented in node.js


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -