I already have an Aws IoT setup.
I have a react native simple app, running in android simulator.
I have implemented this library https://www.npmjs.com/package/react-native-native-mqtt.
I have my pem, crt and key stored in %USERPROFILE%\certs.
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"
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()?