0

1/Producing error:

-File broker.js :

const fs = require('fs')
const aedes = require('aedes')()
const port = 8883    
const options = {
  key: fs.readFileSync('cert/key.pem'),
  cert: fs.readFileSync('cert/crt.pem')
}    
const server = require('tls').createServer(options, aedes.handle)    
server.listen(port, function () {
  console.log('MQTT broker server started and listening on port ', port)
})

-File sub.js :

var mqtt    = require('mqtt');
const fs = require('fs');
var caFile = fs.readFileSync("cert/ca.ca");
//if using client certificates
var KEY = fs.readFileSync('cert/key.pem');
var CERT = fs.readFileSync('cert/crt.pem');
var options={
  clientId:"mqttjs01",
  //port:8883,
  //host:'192.168.1.71',
  //protocol:'mqtts',
  rejectUnauthorized : false,
  ca:caFile,
  key: KEY,
  cert: CERT
}
var client  = mqtt.connect("mqtts://domain.com:8883",options);    
//console.log("connected flag  " + client.connected);    
client.on('connect', function () {
  console.log('Connected');
  client.subscribe('message')
})
client.on('message', function (topic, message) {
    context = message.toString();
    console.log("sub:",topic+'-'+context)
})

-File pub.js :

var mqtt    = require('mqtt');
const fs = require('fs');
var caFile = fs.readFileSync("cert/ca.ca");
//if using client certificates
var KEY = fs.readFileSync('cert/key.pem');
var CERT = fs.readFileSync('cert/crt.pem');
var options={
  clientId:"mqttjs01",
  //port:8883,
  //host:'192.168.1.71',
  //protocol:'mqtts',
  rejectUnauthorized : false,
  ca:caFile,
  key: KEY,
  cert: CERT
}
var client  = mqtt.connect("mqtts://domain.com:8883",options);

//console.log("connected flag  " + client.connected);
var dem =0;
client.on('connect', function () {
    console.log('Connected');    
    setInterval(function() {
        client.publish('message', 'Hello mqtt ' + dem);
        console.log('Message Sent ' + dem);
        dem = dem + 1;
    }, 5000);
});

2/ Error status:

  • File broker.js: Run ok "MQTT broker server started and listening on port 8883"
  • File sub.js: display "Connected" but no value show. -> Expect display "Message Sent ..." every 5 seconds.
  • File pub.js: display "Connected" and display "Message Sent ..." every chaos seconds.

In the past, I set up done with mosca but when change to aedes, It's not run, do I wrong something ?

hardillb
  • 54,545
  • 11
  • 67
  • 105
Tony
  • 493
  • 4
  • 7

1 Answers1

0

With @hardillb helps, I've fixed it. Thus change clientId in sub and pub, not remove that and value can pass.

Tony
  • 493
  • 4
  • 7