shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

Can't connect to an MQTT server that has a username and password #56

Closed tobeedelafuente closed 5 years ago

tobeedelafuente commented 5 years ago

Hello! I am trying to make a flutter app that connects to the MQTT server of meeo.xyz. I verified that I am able to connect to it by setting the credentials using MQTT.fx

screen shot 2018-11-27 at 10 28 49 am

When I try with mqtt_client, I get this error:

flutter: 2018-11-27 10:29:52.135926 -- Authenticating with username '{md-hi75gqj}' and password '{user_K8SzwBbLqBEwfIqM}'
flutter: 2018-11-27 10:29:52.136213 -- Password length (21) exceeds the max recommended in the MQTT spec.
...
flutter: 2018-11-27 10:29:53.880479 -- SynchronousMqttConnectionHandler::_connectAckProcessor
flutter: 2018-11-27 10:29:53.881361 -- SynchronousMqttConnectionHandler::_connectAckProcessor connection rejected
flutter: 2018-11-27 10:29:53.882681 -- SynchronousMqttConnectionHandler:: cancelling connect timer
flutter: 2018-11-27 10:29:53.884647 -- SynchronousMqttConnectionHandler::internalConnect - post sleep, state = Connection status is disconnected with return code badUsernameOrPassword
flutter: 2018-11-27 10:29:53.884821 -- SynchronousMqttConnectionHandler::internalConnect failed
flutter: ERROR: mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement
flutter: ERROR: MQTT client connection failed - disconnecting, state is MqttConnectionState.faulted

Here's my code:

  String broker = "mq.meeo.xyz";
  String username = "md-hi75gqj";
  String password = "some-password";
  void _connect() async {
    client = mqtt.MqttClient(broker, '');
    client.logging(on: true);
    client.onDisconnected = _onDisconnected;
    ....
    final mqtt.MqttConnectMessage connMess = mqtt.MqttConnectMessage()
        .withClientIdentifier('Mqtt_MyClientUniqueId')
        .keepAliveFor(20) // Must agree with the keep alive set above or not set
        .withWillTopic('willtopic') // If you set this you must set a will message
        .withWillMessage('My Will message')
        .startClean() // Non persistent session for testing
        .withWillQos(mqtt.MqttQos.atLeastOnce);
    print('EXAMPLE::Mosquitto client connecting....');
    client.connectionMessage = connMess;
    ...
    try {
      await client.connect(username, password);
    } catch (e) {
      print("ERROR: " + e.toString());
    }
  }

I don't get it as to why I am getting a badUsernameOrPassword error but it is fine in MQTT.fx. Am I missing something for the setup? I've tried setting the useWebsocket as well but I am still having a problem connecting to the MQTT server.

Any help would be deeply appreciated

shamblett commented 5 years ago

Your client id in the example above is .withClientIdentifier('Mqtt_MyClientUniqueId'), however in the picture it seems to be a user generated string, you may need to check the broker docs about how to generate the client id.

tobeedelafuente commented 5 years ago

Thanks for the reply Steve. As long as the client identifier string is unique there is no problem with the broker. MQTT.fx has a feature to generate a unique string though I could use any.

shamblett commented 5 years ago

OK, found the bug, for now add this to your connection message setup

.authenticateAs(username, password);

The username/password passed in to the connect method id not being reflected into the connect method correctly.

tobeedelafuente commented 5 years ago

Thanks Steve! The .authenticateAs(username, password) clearly solved the issue! Kudos to you for this library 👍

Here's the updated code:

  String broker = "mq.meeo.xyz";
  String username = "md-hi75gqj";
  String password = "some-password";
  void _connect() async {
    client = mqtt.MqttClient(broker, '');
    client.logging(on: true);
    client.onDisconnected = _onDisconnected;
    ....
    final mqtt.MqttConnectMessage connMess = mqtt.MqttConnectMessage()
        .withClientIdentifier('Mqtt_MyClientUniqueId')
        .keepAliveFor(20) // Must agree with the keep alive set above or not set
        .withWillTopic('willtopic') // If you set this you must set a will message
        .withWillMessage('My Will message')
        .startClean() // Non persistent session for testing
        .authenticateAs(username, password) // additional code when connecting to a broker w/ creds
        .withWillQos(mqtt.MqttQos.atLeastOnce);
    print('EXAMPLE::Mosquitto client connecting....');
    client.connectionMessage = connMess;
    ...
    try {
      await client.connect(username, password);
    } catch (e) {
      print("ERROR: " + e.toString());
    }
  }