Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tobeedelafuente opened this issue Nov 27, 2018 · 4 comments
Closed

Comments

@tobeedelafuente
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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());
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants