0
votes
  1. I already have an Aws IoT setup.

  2. I have a react native simple app, running in android simulator.

  3. I have implemented this library https://www.npmjs.com/package/react-native-native-mqtt.

  4. I have my pem, crt and key stored in %USERPROFILE%\certs.

  5. I run this command in my cmd mqtt pub --cafile %USERPROFILE%\certs\root-ca.pem --cert %USERPROFILE%\certs\certificate.pem.crt --key %USERPROFILE%\certs\private.pem.key -d -h ******-ats.iot.us-east-2.amazonaws.com -p 8883 -t test -m "hello there"

  6. I checked. It showed up in Aws IoT Test Console, I Subscribed to test.

No error occurring so far.

I have this code in my react native:

const mqtt = require('mqtt')
const fs = require('fs')

// const KEY = fs.readFileSync('%USERPROFILE%\certs\private.pem.key');
// const CERT = fs.readFileSync('%USERPROFILE%\certs\certificate.pem.crt');
// const CAfile = [fs.readFileSync('%USERPROFILE%\certs\root-ca.pem')];
const KEY = './aws/private.pem.key';
const CERT = './aws/certificate.pem.crt';
const CAfile = ['./aws/root-ca.pem'];

const options = {
  host: "******-ats.iot.us-east-2.amazonaws.com",
  port: 8883,
  protocolId: 'MQIsdp',
  ca: CAfile,
  keyPath: KEY,
  certPath: CERT,
  secureProtocol: 'TLSv1_method',
  protocolVersion: 3
};
const client  = mqtt.connect(options)

client.on('connect', function () {
  client.subscribe('test', function (err) {
    if (!err) {
      client.publish('test', 'Hello mqtt')
    }
  })
})

client.on('message', function (topic, message) {
  // message is Buffer
  console.log(message.toString())
  client.end()
})

I have already tried using fs, its causing error fs.readFileSync is not a function
So, I am using this path './aws/private.pem.key' './aws/certificate.pem.crt' './aws/root-ca.pem'

This is the error:

WebSocket connection to 'ws://localhost:8883/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED createBrowserWebSocket @ ws.js:108 browserStreamBuilder @ ws.js:134 wrapper @ index.js:154 ../../../../../../../../../Documents/_msit/iot/Round 2/mqttks/node_modules/mqtt/lib/client.js.MqttClient._setupStream @ client.js:298 ../../../../../../../../../Documents/_msit/iot/Round 2/mqttks/node_modules/mqtt/lib/client.js.MqttClient._reconnect @ client.js:938 (anonymous) @ client.js:958

I was wondering why its localhost even though I set the host.

So, my question, How do I convert this command that is working mqtt pub --cafile %USERPROFILE%\certs\root-ca.pem --cert %USERPROFILE%\certs\certificate.pem.crt --key %USERPROFILE%\certs\private.pem.key -d -h ******-ats.iot.us-east-2.amazonaws.com -p 8883 -t test -m "hello there" into MQTT.js, Javascript, mqtt.connect()?

1

1 Answers

1
votes

host and port are not specified in options. Your connect() call should look something like this:

const HOST = "******-ats.iot.us-east-2.amazonaws.com";
const PORT = 8883;

const options = {
  protocolId: 'MQIsdp',
  ca: CAfile,
  keyPath: KEY,
  certPath: CERT,
  secureProtocol: 'TLSv1_method',
  protocolVersion: 3
};

var uri = "mqtt://" + HOST + ":" + PORT;

const client = mqtt.connect(uri, options);

Your question is similar to what has already been answered here on StackOverflow: Why is MQTT not connecting with NodeJS?