# Connecting to Losant through Mqtt with Flutter App

**URL:** <https://forums.losant.com/t/connecting-to-losant-through-mqtt-with-flutter-app/2513>\
**Category:** Help\
**Tags:** mqtt\
**Created:** [January 2, 2020, 5:44pm UTC](https://forums.losant.com/t/connecting-to-losant-through-mqtt-with-flutter-app/2513 "2020-01-02T17:44:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brindha\_Shayana](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brindha\_Shayana](https://forums.losant.com/u/Brindha_Shayana)\
**Post date:** [January 2, 2020, 5:44pm UTC](https://forums.losant.com/t/connecting-to-losant-through-mqtt-with-flutter-app/2513/1 "2020-01-02T17:44:47Z")

</div>

Hi,

I am able to connect to Losant through an insecure port 1883 with the following details but not able to connect with secure port 8883.

String \_broker = ‘[broker.losant.com](http://broker.losant.com)’;  
int \_port = 1883;

Also when I am connected in the insecure port I am not able to disconnect.

Please advise

1. How to connect on the secure port
2. What could be the issue with disconnect.

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [January 2, 2020, 5:51pm UTC](https://forums.losant.com/t/connecting-to-losant-through-mqtt-with-flutter-app/2513/3 "2020-01-02T17:51:52Z")

</div>

Hi @Brindha_Shayana,

The port for a secure connection is `8883`. If you haven’t, you should check out these docs:

> **[MQTT Protocol](https://docs.losant.com/mqtt/overview/#the-losant-message-broker)**
>
> Learn about communicating with the Losant platform over MQTT.

If you have any more issues connecting to the Losant Broker, any error messages, screenshots, or further information you could provide would be helpful.

---

<div class="post-metadata">

**Author:** ![Brindha\_Shayana](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brindha\_Shayana](https://forums.losant.com/u/Brindha_Shayana)\
**Post date:** [January 2, 2020, 6:19pm UTC](https://forums.losant.com/t/connecting-to-losant-through-mqtt-with-flutter-app/2513/4 "2020-01-02T18:19:40Z")

</div>

Hi

Thanks for your reply. Yes I have been through the Losant documentation. I have been trying this for some time with success just using the insecure port.

I am using the mqtt\_client Dart package (FYI [https://pub.dev/packages/mqtt\_client](https://pub.dev/packages/mqtt_client)) to link my Flutter app with my Losant account. I am able to successfully communicate with Losant on the insecure port 1883. However once I change the port number to 8883 I get the following error.

Error message as below:  
flutter: 2020-01-02 18:07:28.096329 – Authenticating with username ‘{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}’ and password ‘{xxx 64 char password xxx}’  
flutter: 2020-01-02 18:07:28.097055 – Username length (36) exceeds the max recommended in the MQTT spec.  
flutter: 2020-01-02 18:07:28.097405 – Password length (64) exceeds the max recommended in the MQTT spec.  
flutter: 2020-01-02 18:07:28.109708 – SynchronousMqttConnectionHandler::internalConnect entered  
flutter: 2020-01-02 18:07:28.109953 – SynchronousMqttConnectionHandler::internalConnect - initiating connection try 0  
flutter: 2020-01-02 18:07:28.110386 – SynchronousMqttConnectionHandler::internalConnect - insecure TCP selected  
flutter: 2020-01-02 18:07:28.336029 – MqttConnection::\_startListening  
flutter: 2020-01-02 18:07:28.341588 – SynchronousMqttConnectionHandler::internalConnect sending connect message  
flutter: 2020-01-02 18:07:28.349624 – MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.connect  
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0  
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=36000  
Instance of ‘MqttConnectPayload’  
flutter: 2020-01-02 18:07:28.402430 – SynchronousMqttConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified  
[C110.1.1 C478C0B9-81EC-4F58-9FDC-8502F2A16777 2a02:c7f:c33:8800:2018:bdb5:b82a:5f68.59953\<-\>2a00:1450:4009:808::200a.443]  
Connected Path: satisfied (Path is satisfied), interface: en0  
Duration: 1.705s, DNS @1.367s took 0.006s, TCP @1.374s took 0.026s, TLS took 0.055s  
bytes in/out: 5370/1262, packets in/out: 9/10, rtt: 0.027s, retransmitted packets: 0, out-of-order packets: 0  
flutter: 2020-01-02 18:07:28.542708 – MqttConnection::\_onDone - calling disconnected callback

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [January 3, 2020, 6:32pm UTC](https://forums.losant.com/t/connecting-to-losant-through-mqtt-with-flutter-app/2513/5 "2020-01-03T18:32:29Z")

</div>

Hi @Brindha_Shayana,

I have not previously worked with these resources before, but I noticed a couple of lines with issues. Which line is of the most concern?

I want to preface that our expertise lies within Losant, and that I have linked some external documentation below that may solve your error. It may also be beneficial to open a Github issue for the library you are using.

I hope to hear your primary concerns, but I do find it strange that, though you configured a secure connection, the log reads “insecure TCP selected,” which _could_ point to a configuration error within your client. If the following articles do not provide clarity, I would recommend following up with some screenshots of your configuration.

> <https://github.com/shamblett/mqtt_client/issues/93>
>
> I am trying to connect to a local broker on MQTT and getting the following exception, could you please check and...

  

> <https://github.com/shamblett/mqtt_client/issues/77>

  

> <https://github.com/shamblett/mqtt_client/blob/master/example/iot_core.dart>

Thanks!  
Julia

---

<div class="post-metadata">

**Author:** ![Brindha\_Shayana](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@Brindha\_Shayana](https://forums.losant.com/u/Brindha_Shayana)\
**Post date:** [January 4, 2020, 9:59am UTC](https://forums.losant.com/t/connecting-to-losant-through-mqtt-with-flutter-app/2513/6 "2020-01-04T09:59:48Z")

</div>

Hi,

Thanks for your reply. The resources where useful.

I am now able to connect through the secure WebSockets transport (wss://broker.losant.com:443)

It looks like the Flutter mqtt\_client package does not support TLS port 8883.

Regards,  
Sathya
