I am trying to connect to an Mqtts broker using private and public RSA keys pem files, and a signed certificate from the broker. I am getting a weird error saying no start line
. I have tried to go through the documentation and GitHub issues but have had no luck so far.
Can anyone help me here? I belive there is some major key point that I am missing. Is the format of private keys need to be changed? I am giving pem files. with which the certificate is already signed.
const mqtt = require('mqtt');
const fs = require('fs');
// MQTTs broker connection string and port
const brokerUrl = 'mqtts://data-broker-test.some.network';
const brokerPort = 1883;
const clientId = 'develop1'
// Path to your certificate file
const certPath = 'dev1.crt';
//private public key paths rsa
const privateKeyPath = 'proj_dev01.pem'
const publicKeyPath = 'proj_dev01.pub.pem'
// MQTT client options
const options = {
port: brokerPort,
key: fs.readFileSync(privateKeyPath),
cert: fs.readFileSync(publicKeyPath),
ca: [fs.readFileSync(certPath)],
rejectUnauthorized: false,
clientId: clientId,
connectTimeout: 5 * 1000,
reconnectPeriod: 5 * 1000,
};
// Create MQTTs client
const client = mqtt.connect(brokerUrl, options);
// MQTT client event handlers
client.on('connect', () => {
console.log('Connected to MQTTs broker');
client.subscribe();
});
client.on('message', (topic, message) => {
console.log('Received message:', message.toString());
});
client.on('error', (error) => {
console.error('MQTT error:', error);
});
client.on('close', () => {
console.log('Disconnected from MQTT broker');
});
// Handle process termination
process.on('SIGINT', () => {
client.end();
process.exit();
});
error
Error: error:0909006C:PEM routines:get_name:no start line
at node:internal/tls/secure-context:70:13
at Array.forEach (<anonymous>)
at setCerts (node:internal/tls/secure-context:68:3)
at configSecureContext (node:internal/tls/secure-context:157:5)
at Object.createSecureContext (node:_tls_common:116:3)
at Object.connect (node:_tls_wrap:1651:48)
at Object.buildBuilder (/Users/xxx/xxx/xxx/node_modules/mqtt/lib/connect/tls.js:20:26)
at MqttClient.wrapper [as streamBuilder] (/Users/xxx/xxx/xxx/node_modules/mqtt/lib/connect/index.js:155:36)
at MqttClient._setupStream (/Users/xxx/xxx/xxx/node_modules/mqtt/lib/client.js:415:22)
at new MqttClient (/Users/xxx/xxx/xxx/node_modules/mqtt/lib/client.js:395:8) {
library: 'PEM routines',
function: 'get_name',
reason: 'no start line',
code: 'ERR_OSSL_PEM_NO_START_LINE'
}