0
var mqtt = require('mqtt')
var options = {
  username: 'abc',
  password: 'xyz',
}
var client  = mqtt.connect('mqtt:localhost:1883', options);

function authenteClient() {
      // I need to call this function against the callback at server's 
      // authenticate function.
}

In Above code I am providing username and password in options to this mqtt client.

var mosca = require('mosca');
var ascoltatore = {
    type: 'mongo',
    url: 'mongodb://localhost:27017/mqtt',
    pubsubCollection: 'ascoltatori',
    mongo: {}
    };

var settings = {
  port: 1883,
  backend: ascoltatore 
};
var server = new mosca.Server(settings);
server.on('ready', setup);

function setup() {
  server.authenticate = authenticate;
  console.log('Mosca server is up and running');
}

var authenticate = function(client, username, password, callback) {
   console.log(username, password);
   callback(true);
}

Here in server side in the authenticate function, I need to connect a callback at client side that is being called there as callback(true).

Ahmad Raza
  • 21
  • 7
  • It is not clear what you are asking here. – hardillb Apr 19 '18 at 06:43
  • there is an `authenticate` function at server side that is being called while connecting it. You can see the `callback(true)` in the `authenticate` function. I need to call my function `authenteClient` at client side when the `callback(true)` is invoked. – Ahmad Raza Apr 19 '18 at 06:48

2 Answers2

0

If the client fails the authentication on the broker side it will not connect.

If it passes it will connect and you can be notified by using the client.on('connect',function(){}) event listener.

hardillb
  • 54,545
  • 11
  • 67
  • 105
0

Late to party but on the client side you can do something like this:

//Handle errors
client.on("error", (error) => {
  console.log("Error: ", error.message);
});

Any errors from the server can be handled gracefully at this stage.

Max BigBudget
  • 71
  • 1
  • 8