Question: Is it possible to use client certificate for authentication in paho-mqtt javascript client?
From the application point of view, you will have only one option (set useSSL to true). Then Paho JavaScript client will establish a secure websocket connection (i.e. wss://) . For two authentication, client requires a certificate to establish the connection.
Is it possible to use client authentication with a certificate instead of username/password with the Paho javascript client ? If so, how? please share the tutorials and documentation to proceed.
x2
I have the same question
@bharanidharan81 Any update on this issue? Have you found the solution?? Is there anyone who has a solution as I'm also looking for the same.
looking for the solution
I solved it by using browserMqtt.js based on MQTT.js https://www.npmjs.com/package/mqtt
I got the browserMqtt.js by these steps:
npm install mqtt --save
npm install -g browserify
npm install -g webpack-cli
npm install -g webpack@4
cd node_modules/mqtt
npm install .
browserify mqtt.js -s mqtt > browserMqtt.js
webpack mqtt.js ./browserMqtt.js --output-library mqtt
the server side is using mosquitto the client side config using cert file is
<script src="./browserMqtt.js"></script>
<script>
// var KEY = '/var/www/html/test02/client-key.key';
// var CERT = '/var/www/html/test02/client-cert.crt';
// var TRUSTED_CA_LIST = '/var/www/html/test02/cacert.crt';
// var PORT = '8883';
// var HOST = '159.210.65.6';
var options = {
port: '8081',
host: '159.210.65.6',
keyPath: '/var/www/html/test02/client-key.pem',
certPath: '/var/www/html/test02/client-cert.pem',
rejectUnauthorized : false,
//The CA list will be used to determine if server is authorized
ca: ['/var/www/html/test02/cacert.pem'],
protocol: 'wss',
protocolId: 'MQTT',
// username: 'qqq',
// password: 'bbb',
clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8)
};
var client = mqtt.connect(options);
client.subscribe('messages');
client.publish('messages', 'Current time is: ' + new Date());
client.on('message', function(topic, message) {
console.log(message);
});
client.on('connect', function(){
console.log('Connected');
});
</script>
From the application point of view, you will have only one option (set useSSL to true). Then Paho JavaScript client will establish a secure websocket connection (i.e. wss://) . For two authentication, client requires a certificate to establish the connection.
Is it possible to use client authentication with a certificate instead of username/password with the Paho javascript client ? If so, how? please share the tutorials and documentation to proceed.
have found a response ? I try to establish connection with client certificate in react native app
I solved it by using browserMqtt.js based on MQTT.js https://www.npmjs.com/package/mqtt
I got the browserMqtt.js by these steps:
npm install mqtt --save npm install -g browserify npm install -g webpack-cli npm install -g webpack@4 cd node_modules/mqtt npm install . browserify mqtt.js -s mqtt > browserMqtt.js webpack mqtt.js ./browserMqtt.js --output-library mqttthe server side is using mosquitto the client side config using cert file is
<script src="./browserMqtt.js"></script> <script> // var KEY = '/var/www/html/test02/client-key.key'; // var CERT = '/var/www/html/test02/client-cert.crt'; // var TRUSTED_CA_LIST = '/var/www/html/test02/cacert.crt'; // var PORT = '8883'; // var HOST = '159.210.65.6'; var options = { port: '8081', host: '159.210.65.6', keyPath: '/var/www/html/test02/client-key.pem', certPath: '/var/www/html/test02/client-cert.pem', rejectUnauthorized : false, //The CA list will be used to determine if server is authorized ca: ['/var/www/html/test02/cacert.pem'], protocol: 'wss', protocolId: 'MQTT', // username: 'qqq', // password: 'bbb', clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8) }; var client = mqtt.connect(options); client.subscribe('messages'); client.publish('messages', 'Current time is: ' + new Date()); client.on('message', function(topic, message) { console.log(message); }); client.on('connect', function(){ console.log('Connected'); }); </script>
@nijisakai is this working, can we use this
I solved it by using browserMqtt.js based on MQTT.js https://www.npmjs.com/package/mqtt I got the browserMqtt.js by these steps:
npm install mqtt --save npm install -g browserify npm install -g webpack-cli npm install -g webpack@4 cd node_modules/mqtt npm install . browserify mqtt.js -s mqtt > browserMqtt.js webpack mqtt.js ./browserMqtt.js --output-library mqttthe server side is using mosquitto the client side config using cert file is
<script src="./browserMqtt.js"></script> <script> // var KEY = '/var/www/html/test02/client-key.key'; // var CERT = '/var/www/html/test02/client-cert.crt'; // var TRUSTED_CA_LIST = '/var/www/html/test02/cacert.crt'; // var PORT = '8883'; // var HOST = '159.210.65.6'; var options = { port: '8081', host: '159.210.65.6', keyPath: '/var/www/html/test02/client-key.pem', certPath: '/var/www/html/test02/client-cert.pem', rejectUnauthorized : false, //The CA list will be used to determine if server is authorized ca: ['/var/www/html/test02/cacert.pem'], protocol: 'wss', protocolId: 'MQTT', // username: 'qqq', // password: 'bbb', clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8) }; var client = mqtt.connect(options); client.subscribe('messages'); client.publish('messages', 'Current time is: ' + new Date()); client.on('message', function(topic, message) { console.log(message); }); client.on('connect', function(){ console.log('Connected'); }); </script>@nijisakai is this working, can we use this
It works fine with my project