6

I am trying to connect IBM Watson IoT platform using Paho MQTT Javascript client as mentioned in the below example code.

 
 var client = new Messaging.Client("myOqgId.messaging.internetofthings.ibmcloud.com", 8883, "myclientid_" + parseInt(Math.random() * 100, 10));

 //Gets  called if the websocket/mqtt connection gets disconnected for any reason
 client.onConnectionLost = function (responseObject) {
     //Depending on your scenario you could implement a reconnect logic here
     alert("connection lost: " + responseObject.errorMessage);
 };

 //Gets called whenever you receive a message for your subscriptions
 client.onMessageArrived = function (message) {
     //Do something with the push message you 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 the connection has sucessfully been established
     onSuccess: function () {
         alert("Connected");
     },
     //Gets Called if the connection could not be established
     onFailure: function (message) {
         alert("Connection failed: " + message.errorMessage);
     }
 };

 //Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
 var publish = function (payload, topic, qos) {
     //Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
     var message = new Messaging.Message(payload);
     message.destinationName = topic;
     message.qos = qos;
     client.send(message);
 }

But not able to connect. I am getting this error: WebSocket connection to 'ws://myOrgIdXYZ.messaging.internetofthings.ibmcloud.com:8883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

Please anybody out there who tried connecting IBM Watson IoT using Paho Mqtt client.

  • Shouldn't the client id be something defined in the IoT service rather than a randomly generated value? – hardillb Apr 06 '17 at 11:42
  • Client id will be different for each user so it is dynamically generated. – vinayakumarnk Apr 06 '17 at 12:00
  • But check the IoT Service doc, I'm sure the client id needs to match something declared in the service (at least for devices and gateways) – hardillb Apr 06 '17 at 12:02
  • I am using as Application. So client id should be different for each user. I am already using same kind of client id in Node js client application. – vinayakumarnk Apr 06 '17 at 12:15
  • Port 8883 is secure, so I would expect wss as protocol. What happens if you try 1883? Agree with you on clientid for apps, they just have to be unique per connection so generating is common enough. – amadain Apr 06 '17 at 12:24
  • I tried both 8883 and 1883. In both cases I could not connect. I am getting this error "Connection failed: AMQJS0006E Bad Connack return code:5 Connection Refused: not authorized". – vinayakumarnk Apr 06 '17 at 13:11
  • 1
    @hardillb is right - the docs say: "When you make an MQTT connection by using an API key, ensure that the following guidelines are applied: The MQTT client ID is in the format: a:orgId:appId ..." - see https://console.ng.bluemix.net/docs/services/IoT/applications/mqtt.html – DisappointedByUnaccountableMod Apr 06 '17 at 14:57

1 Answers1

5

Thank you all for your responses. Based on all responses I have made changes in my 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 the websocket/mqtt connection gets disconnected for any reason
 client.onConnectionLost = function (responseObject) {
     //Depending on your scenario you could implement a reconnect logic here
     alert("connection lost: " + responseObject.errorMessage);
 };

 //Gets called whenever you receive a message for your subscriptions
 client.onMessageArrived = function (message) {
     //Do something with the push message you 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 the connection has sucessfully been established
     onSuccess: function () {
         alert("Connected");      
     },
     //Gets Called if the connection could not be established
     onFailure: function (message) {
         alert("Connection failed: " + message.errorMessage);
     }
 };

 //Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
 var publish = function (payload, topic, qos) {
     //Send your message (also possible to serialize it as JSON or protobuf or just use a 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 that I have made changes in ClientId. IBM Watson Iot will accept only the client ids in the below format if you are not using Watson IoT libraries.

var clientId = 'a:OrgId:'+RandomString;

If you are using IBM Watson IoT libraries Client Id can be anything. Even I implemented in node.js