moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 507 forks source link

mosca needs restart after new user #489

Closed benu2016 closed 8 years ago

benu2016 commented 8 years ago

Hello, I'm struggeling with peace of code:

function adduser(username, password, authorizePublish, authorizeSubscribe) { loadAuthorizerAndSave(function(err, authorizer, done) { authorizer.addUser(username, password, authorizePublish, authorizeSubscribe, done); }); }

function loadAuthorizerAndSave(cb) { loadAuthorizer(moscaSettings, function (err, authorizer) { if (err) { authorizer = new Authorizer(); } cb(null, authorizer, function(err) { if (err) throw (err); fs.writeFile(moscaSettings.mqtt_credentials, JSON.stringify(authorizer.users, null, 2), function(err){ if (err) throw err; console.log('User saved'); }); }); }); }

function loadAuthorizer(program, cb) { if (program.mqtt_credentials) { fs.readFile(program.mqtt_credentials, function(err, data) { if (err) { cb(err); return; } var authorizer = new Authorizer(); try { authorizer.users = JSON.parse(data); cb(null, authorizer); } catch(err) { cb(err); } }); } else { cb(null, null); } }

adding user works fine, but server needs restart to recognize user. Can you help me ?

mcollina commented 8 years ago

You need to send SIGHUP to reload the user.

kill -1 PID.

benu2016 commented 8 years ago

How can I do that in node ? Do you have an example ?

benu2016 commented 8 years ago

I figured out how: process.kill(process.pid,'SIGHUP'); But now I get this error: Hangup: 1 End then the server stops. So where is the difference ?

mcollina commented 8 years ago

Please upload a full example, it's hard to tell. Il giorno sab 11 giu 2016 alle 13:21 benu2016 notifications@github.com ha scritto:

I figured out how: process.kill(process.pid,'SIGHUP'); But now I get this error: Hangup: 1 End then the server stops. So where is the differece ?

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/mcollina/mosca/issues/489#issuecomment-225354064, or mute the thread https://github.com/notifications/unsubscribe/AADL4wEhD2THWrsrTuWIKMsWcWrG-yn6ks5qKppIgaJpZM4IyW9n .

benu2016 commented 8 years ago

`var mosca = require('mosca'); var Authorizer = require('mosca/lib/authorizer'); var fs = require("fs"); var process = require('process'); //var credentials = './credentials.json'; var authorizer = new Authorizer(); var moscaSettings = { https: { port: 3000, bundle: true, static: './' }, secure: { keyPath: 'key.pem', certPath: 'cert.pem' }, mqtt_credentials: __dirname + '/credentials.json' };

var server = new mosca.Server(moscaSettings); server.on('ready', setup);

server.on('clientConnected', function(client) { console.log('client connected', client.id); console.log('client Info', client.username, client.password); });

server.on('published', function(packet, client) { console.log('published:'+packet.payload+' '+ packet.topic); if(packet.topic == '/registration'){ console.log('Registration request!!!'); var userInfo = JSON.parse(packet.payload.toString()); adduser(userInfo.userName, userInfo.passWord, userInfo.authorizePublish, userInfo.authorizeSubscribe); }

});

server.on('clientDisconnected', function(client) { console.log('Client Disconnected:', client.id); });

function adduser(username, password, authorizePublish, authorizeSubscribe) { loadAuthorizerAndSave(function(err, authorizer, done) { authorizer.addUser(username, password, authorizePublish, authorizeSubscribe, done); }); }

function loadAuthorizerAndSave(cb) { loadAuthorizer(moscaSettings, function (err, authorizer) { if (err) { authorizer = new Authorizer(); }

  cb(null, authorizer, function(err) {
    if (err) throw (err);
    fs.writeFile(moscaSettings.mqtt_credentials, JSON.stringify(authorizer.users, null, 2), function(err){
        if (err) throw err;
        console.log('User saved');
    });
  });
});

}

function loadAuthorizer(program, cb) { if (program.mqtt_credentials) { fs.readFile(program.mqtt_credentials, function(err, data) { if (err) { cb(err); return; }

  var authorizer = new Authorizer();

  try {
    authorizer.users = JSON.parse(data);
    cb(null, authorizer);
  } catch(err) {
    cb(err);
  }
});

} else { cb(null, null); } }

function setup() { adduser('admin', 'admin', '/registration', '/registration'); console.log('PID', process.pid); loadAuthorizer(moscaSettings, function(err, authorizer) { if (err) { console.log('Error:',err); }

    if (authorizer) {
        server.authenticate = authorizer.authenticate;
        server.authorizeSubscribe = authorizer.authorizeSubscribe;
        server.authorizePublish = authorizer.authorizePublish;
    }
});    
console.log('Mosca server is up and running')

}`

mcollina commented 8 years ago

You forgot to copy https://github.com/mcollina/mosca/blob/master/lib/cli.js#L170-L194.