shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
543 stars 175 forks source link

Is there a way to use MQTT over TLS #199

Closed ldemyanenko closed 4 years ago

ldemyanenko commented 4 years ago

I didn't find a way yo use MQTT over TLS.

I'm getting error mqtt connect error: SocketException: Failed host lookup: 'mqtt://yyy.xxx.net/mqtt' (OS Error: No address associated with hostname, errno = 7)

I'm able to connect to the MQTT server through MQTT Explorer app with TLS encryption but wasn't able to connect from flutter using mqtt_client.

Please help me with this issue

shamblett commented 4 years ago

Not sure what you mean by 'over TLS', the mqtt_server_client supports connecting over secure sockets, see the example mqtt_server_client_secure.dart in the examples directory. The browser client supports only secure webcockets(wss).

ldemyanenko commented 4 years ago

I mean something like this

MQTT and TLS MQTT relies on the TCP transport protocol. By default, TCP connections do not use an encrypted communication. To encrypt the whole MQTT communication, many MQTT brokers (such as HiveMQ) allow use of TLS instead of plain TCP. If you use the username and password fields of the MQTT CONNECT packet for authentication and authorization mechanisms, you should strongly consider using TLS.

Port 8883 is standardized for a secured MQTT connection. The standardized name at IANA is “secure-mqtt”. Port 8883 is exclusively reserved for MQTT over TLS.

https://www.hivemq.com/blog/mqtt-security-fundamentals-tls-ssl/

ldemyanenko commented 4 years ago

Screenshot 2020-07-08 at 10 00 18 I'm able to set the connection with MQTT Explorer using TLS setting. But didn't find this option in mqtt_client. I'm trying to use mqtt_client in mobile app

shamblett commented 4 years ago

Yes, then you need to use the mqtt_server_client configured to use secure sockets as I said above.

ldemyanenko commented 4 years ago

Do you have an example for this case?

shamblett commented 4 years ago

Yes, as I've said above ' see the example mqtt_server_client_secure.dart in the examples directory'.

ldemyanenko commented 4 years ago

Was able to connect with

secure  = true
no certificate
port 8883
lyxia commented 3 years ago

Was able to connect with

secure  = true
no certificate
port 8883

@ldemyanenko I have the same problem, can you give me a demo address or code snippet? Thank you

BBarisKilic commented 4 months ago

Was able to connect with

secure  = true
no certificate
port 8883

@ldemyanenko I have the same problem, can you give me a demo address or code snippet? Thank you

As @shamblett mentioned there is an example dart file: https://github.com/shamblett/mqtt_client/blob/master/example/mqtt_server_client_secure.dart

If you want to activate TLS without certificates, it is correct that secure should be true but also the onBadCertificate should be assigned as follows as in the example:

client
  ..secure = true
  ..port = 8883
  ..onBadCertificate = (Object a) => true;