2

I've been trying to use the javacscript version of the Eclipse Paho MQTT client to access the Google IOTCore MQTT Bridge, as suggested here:

https://cloud.google.com/iot/docs/how-tos/mqtt-bridge

However, whatever I do, any attempt to connect with known good credentials (working with other clients) results in this connection error:

errorCode: 7, errorMessage: "AMQJS0007E Socket error:undefined."

Not much to go on there, so I'm wondering if anyone has ever been successful connecting to the MQTT Bridge via Javascript with Eclipse Paho, the client implementation suggested by Google in their documentation.

I've gone through their troubleshooting steps, and things seem to be on the up and up, so no help there either.

https://cloud.google.com/iot/docs/troubleshooting

I have noticed that in their docs they have sample code for Java/Python, etc, but not Javascript, so I'm wondering if it's simply not supported and their documentation just fails to mention as such.

I've simplified my code to just use the 'Hello World' example in the Paho documentation, and as far as I can tell I've done things correctly (including using my device path as the ClientID, the JWT token as the password, specifying an 'unused' userName field and explicitly requiring MQTT v3.1.1).

In the meantime I'm falling back to polling via their HTTP bridge, but that has obvious latency and network traffic shortcomings.

// Create a client instance
client = new Paho.MQTT.Client("mqtt.googleapis.com", Number(8883), "projects/[my-project-id]/locations/us-central1/registries/[my registry name]/devices/[my device id]");

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({
    mqttVersion: 4,   // maps to MQTT V3.1.1, required by IOTCore
    onSuccess:onConnect,
    onFailure: onFailure,
    userName: 'unused',  // suggested by Google for this field
    password: '[My Confirmed Working JWT Token]' // working JWT token

function onFailure(resp) {
    console.log(resp);
}


// 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);
}

2 Answers2

2

I'm a Googler (but I don't work in Cloud IoT).

Your code looks good to me and it should work. I will try it for myself this evening or tomorrow and report back to you.

I've spent the past day working on a Golang version of the samples published on Google's documentation. Like you, I was disappointed to not see all Google's regular languages covered by samples.

Are you running the code from a browser or is it running on Node.JS?

Do you have a package.json (if Node) that you would share too please?

Update

Here's a Node.JS (JavaScript but non-browser) that connects to Cloud IoT, subscribes to /devices/${DEVICE}/config and publishes to /devices/${DEVICE}/events.

https://gist.github.com/DazWilkin/65ad8890d5f58eae9612632d594af2de

  • Place all the files in the same directory
  • Replace values in index.js of the location of Google's CA and your key
  • Replaces [[YOUR-X]] values in config.json
  • Use "npm install" to pull the packages
  • Use node index.js

You should be able to pull messages from the Pub/Sub subscription and you should be able to send config messages to the device.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • 3
    I spoke with the Engineering team and it is *not* possible to use JavaScript to connect to Cloud IoT Core. This is because Cloud IoT Core doesn't currently support using WebSockets (which is what the JavaScript MQTT library uses) to connect to Cloud IoT Core. – DazWilkin Apr 20 '18 at 17:09
  • 2
    That was my suspicion, thank you for taking the time to add this confirmation. – SantaCruzDeveloper Jun 20 '18 at 22:04
  • @DazWilken is this still the case? I'm facing the same error using the [Paho JS library](https://github.com/eclipse/paho.mqtt.javascript). I tried to follow [this tutorial](https://motion-software.com/blog/how-to-use-mqtt-with-react-native/) to publish messages to a topic on GCP Core IoT from my React Native app. The [GCP Cloud IoT Core docs](https://cloud.google.com/iot/docs/how-tos/mqtt-bridge#using_a_long-term_mqtt_domain) mention the Eclipse Paho project but don't seem to go into a lot of detail. – Brad W Jan 13 '20 at 15:00
  • @brad-w -- I've not used Cloud IoT for some time and am unable to answer your question – DazWilkin Jan 15 '20 at 19:00
1

Short answer is no. Google Cloud IoT Core doesn't support WebSockets. All the JavaScript MQTT libraries use WebSocket because JavaScript is restricted to perform HTTP requests and WebSocket connections only.

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159