shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

Support WebAssembly compilation #528

Closed NotTsunami closed 1 week ago

NotTsunami commented 7 months ago

This is a PR to address #503 and support WebAssembly compilation. Due to the testing required and additional work to be done, this PR was opened as a draft.

What this commit does:

What work still needs to get done:

NotTsunami commented 6 months ago

I should have the bandwidth to do some proper testing on this during this coming weekend (as well as resolve the conflict). I'm still not familiar enough with AWS to know rather the policy attachment code is still necessary/how to fix.

shamblett commented 6 months ago

Ok thanks leave AWS for now we can address this later when we know we have a good working package.

NotTsunami commented 3 months ago

@shamblett Hey Steve. I'm back on this again because we have returned to using MQTT at my work, so I am able to work on this as an extension of my work. I rebased this yesterday and reapplied the changes, but I'm noticing something very odd. We are using the nanomq broker (version 0.22.2), and I am unable to connect like normal on the web side at all (Not using webassembly yet - just the regular Flutter web compiler). My connection setup looks like:

Future<void> initializeClient(String mClientID) async {
  final connMessage = MqttConnectMessage()
      .withClientIdentifier(mClientID)
      .withWillTopic('will')
      .withWillMessage('Client disconnected unexpectedly')
      .withWillQos(MqttQos.atLeastOnce)
      .startClean();

  client = MqttBrowserClient('ws://127.0.0.1/mqtt', mClientID)
    ..onConnected = onConnected
    ..onDisconnected = onDisconnected
    ..onUnsubscribed = onUnsubscribed
    ..onSubscribed = onSubscribed
    ..onSubscribeFail = onSubscribeFail
    ..pongCallback = pong
    ..keepAlivePeriod = 60
    ..port = 8083
    ..connectionMessage = connMessage
    ..autoReconnect = true
    ..websocketProtocols = MqttClientConstants.protocolsSingleDefault
    ..setProtocolV311();

  dPrint('MQTT_LOGS:: MQTT client connecting....');

  try {
    await client?.connect();
  } catch (e) {
    dPrint('MQTT_LOGS:: $e');
  }
}

I enabled logging, and I noticed the protocol version in the connection string was 3 instead of the expected 4. I changed the default in mqtt_client_protocol.dart to the V311 constants, and it instantly connected. Is there any reason it might not be respecting setProtocolV311()?

Aside from that, I'm still getting an exception when compiling to WebAssembly even with the local change to mqtt_client_protocol.dart, so this still isn't ready yet. I did clean up the AWS implementation to fully drop sigv4, but I don't have an environment to test this (yet).

shamblett commented 3 months ago

Hi Tyler.

Setting the ..setProtocolV311(); should work however whilst checking I've noticed a bug in the code, this only works if you declare your connect message after creating your client, i.e. in your code above put the connmessage after the browserclient creation.

I'll fix this, also I think I'll make 311 the default rather than 3, some brokers(as you have seen) are more sensitive to this than others. Most users/brokers will expect 311 these days.

Thanks for the web assembly work BTW. Please post any client logs you may have if you want me to look at anything.

shamblett commented 1 week ago

Merged this, any further work identified will be carried out on branch issue503