moscajs / mosca

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

Get userName on publish #602

Closed RobertoBiundo closed 7 years ago

RobertoBiundo commented 7 years ago

When the user authenticates he uses a userName and password. Example from mosca mosquitto_pub -h localhost -m 123 -t topic -u user@name.com -P 1234

is there a way to obtain this userName on the on Publish server.on('published', function (packet, client) { }

i checked the client object but i can't seem to find anything that would give me his name. I need this for further processing of the package.

Any advice?

mcollina commented 7 years ago

you need to setup the authorize callbacks, and store the username inside the client if you need it.

RobertoBiundo commented 7 years ago

how do i mach the client in the on publish call to the ones i saved? is there a reliable id to do so?

RobertoBiundo commented 7 years ago

Ohh...i see...thanks. I found it already some code for future reference

server.authenticate = function (client, username, password, callback) {
        client.user = username; // store username here
        callback(null, true); // authenticate user before sending true
    }
    server.on('published', function (packet, client) {
    // client will contain the userName client.user
    }