shamblett / mqtt_client

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

MqttBrowserClient wss connection #347

Closed carlosdomenje closed 2 years ago

carlosdomenje commented 2 years ago

Hello, I am working with Flutter web and I need to connect to my mqtt broker developed with RabbitMQ through a secure connection using certificates. This is the implementation I do. https://www.rabbitmq.com/web-mqtt.html#tls

When I try to communicate with the broker it tells me that there are no certificates in the connection, which is logical because I don't know where to attach them in the connection through websockets

Using the MqttServerClient client and compiling for iOS or Android I can use the certificates correctly, but I don't know how to establish a secure connection with these certificates using MqttBrowserClient to compile for the web. Could you guide me if possible? Thanks a lot.

This is my code:

`

final mqttClient = MqttBrowserClient(
  'wss://my-broker-url/ws', '');
final mqttConnMess = MqttConnectMessage()
  .authenticateAs('user-id', 'pass-id')
  .withWillQos(MqttQos.atLeastOnce);

mqttClient.port = 15676;
mqttClient.keepAlivePeriod = 60;

mqttClient.autoReconnect = true;
mqttClient.websocketProtocols = MqttClientConstants.protocolsSingleDefault;

mqttClient.setProtocolV311();

mqttClient.onConnected = onConnected;

mqttClient.onDisconnected = onDisconnected;

mqttClient.pongCallback = onPong;

mqttClient.connectionMessage = mqttConnMess;

mqttClient.clientIdentifier = uuid.v1().toString();

try {
  await mqttClient.connect();
} on NoConnectionException catch (e) {
  print(e);
  mqttClient.disconnect();
} on SocketException catch (e) {
  print(e);
  mqttClient.disconnect();
}

` From Broker Side i get this response.

` mqtt-server | 2022-01-05 13:37:42.683374+00:00 [noti] <0.943.0> TLS server: In state wait_finished received CLIENT ALERT: Fatal - Certificate Unknown mqtt-server | 2022-01-05 13:37:42.683374+00:00 [noti] <0.943.0> mqtt-server | 2022-01-05 13:37:42.808120+00:00 [noti] <0.949.0> TLS server: In state wait_finished received CLIENT ALERT: Fatal - Certificate Unknown

`

shamblett commented 2 years ago

There is no Security Context facility for the browser client as you probably know all you get is what the Dart runtime provides for wss connections. You probably need to install a cert , see here.

carlosdomenje commented 2 years ago

Thank you for your response. Now it is clear for me.