mqttjs / MQTT.js

The MQTT client for Node.js and the browser
Other
8.62k stars 1.42k forks source link

Default clientId not used with `clean: false` #366

Closed nfantone closed 8 years ago

nfantone commented 8 years ago

Docs state that, if unset, the default value of options.clientId should be:

clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8).

And, indeed, I've noticed that if you create a MqttClient instance, its constructor proceeds like so:

this.options.clientId = this.options.clientId || defaultId();

However, if you follow the example in the README.md and call .connect() directly on the module with clean: false as an option

var client = mqtt.connect('mqtt://rabbit.dev.contentamobile.com.ar:1883', {
  username: 'contenta',
  password: 'contenta',
  clean: false
});

It fails before even creating the client with:

/home/nfantone/dev/js/tfo/tfo-ms-contact/node_modules/mqtt/lib/connect/index.js:112
    throw new Error('Missing clientId for unclean clients');
    ^

Error: Missing clientId for unclean clients
    at Object.connect (/home/nfantone/dev/js/tfo/tfo-ms-contact/node_modules/mqtt/lib/connect/index.js:112:11)

Which is, actually, expected as per connect/index.js:112:

  if (false === opts.clean && !opts.clientId) {
    throw new Error('Missing clientId for unclean clients');
  }

Is this by design/intentional? What's the catch?

Thank you.

JuanGarassino commented 8 years ago

+1

nfantone commented 8 years ago

Ok, nevermind. I think I got it. clientId should be the same on every connection in order to retrieve offline messages.

Thanks, anyway.