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

Support WebAssembly compilation #528

Merged
merged 2 commits into from
Nov 12, 2024
Merged

Conversation

NotTsunami
Copy link

@NotTsunami NotTsunami commented Apr 3, 2024

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:

  • Upgrades dependencies to their minimum necessary version to support pkg:web. This should support pkg:web 0.4.2 <= 0.5.1, so Flutter users on Flutter 3.19.5 should be able to use the project, as well as Flutter users on Flutter master
    • As a follow up to this, it pushes the required Dart version to at least 3.3.0. This is shipped with Flutter 3.19, so this is fine, but this definitely requires a major version increase from this library.
  • d7eec87 is reverted because I do not believe this is necessary (it is also a blocker in moving to pkg:web). MqttBrowserClient is a class purely targeted for browsers, so it should not be handling non-browser environments. I am currently using mqtt_client in a monorepo solution that targets desktop + web, and was able to solve fix(lib/mqtt_browser_client): switch dart:html to universal_html #509 using conditional imports.
  • The old example/aws_iot_cognito.dart is deleted, and the sigv4 dependency is removed. AWS Amplify is the suggested framework for working with AWS now, along with it being first party. The recently added aws_iot_cognito_amplify.dart does a lot of heavy lifting and showcases how to use MQTT with AWS. Dropping example/aws_iot_cognito.dart is probably acceptable.

What work still needs to get done:

  • Testing! And more testing. This needs to be thoroughly tested in multiple environments and workflows to ensure nothing is broken. WebAssembly compilation for Flutter is very new, and has a lot of moving parts, hence why the version constraint on pkg:web is so tight. It can probably be loosened in the future.
  • In example/aws_iot_cognito_amplify.dart, the portion regarding the policy attachment has been commented out as it relied on the now-removed sigv4 dependency. I'm not familiar enough with AWS or AWS Amplify to know if this portion is still necessary or how to update it with AWS Amplify functions.

@NotTsunami
Copy link
Author

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

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

@NotTsunami
Copy link
Author

NotTsunami commented Aug 9, 2024

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

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 shamblett changed the base branch from master to issue503 November 8, 2024 10:32
@shamblett shamblett marked this pull request as ready for review November 12, 2024 10:25
@shamblett shamblett merged commit 3cfc73b into shamblett:issue503 Nov 12, 2024
2 checks passed
@shamblett
Copy link
Owner

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

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

Successfully merging this pull request may close these issues.

2 participants