fzyzcjy/flutter_rust_bridge

Support Web (besides already-supported Android, iOS, Linux, Windows, macOS)

Closed this issue ยท 10 comments

This library already supports Android, iOS, Linux, Windows, macOS. Now comes the last one: Web.

Why

Many, many people are using webpages. And with this last piece, we will support all platforms that Flutter supports.

Feasibility

As is always said, this lib does nothing but simply generate some code that a human being will otherwise write down manually. So there should be no problem as long as one can make a human-written-down ffi work.

I have not done very thorough research yet, but there already seems to be something that we can use:

  • Rust can be compiled into WebAssembly
  • Websites can run WebAssembly
  • https://pub.dev/packages/web_ffi says that it is "a drop-in solution for using dart:ffi on the web", and our automatically generated code mainly relies on dart:ffi.

Possible steps

  1. Firstly make a minimal working sample that makes Flutter Web calls Rust, without flutter_rust_bridge, possibly using package:web_ffi.
  2. Then it should be trivial to add support for flutter_rust_bridge.

Last time I did something like this I actually did something very different: I would separate the native frontend from the WASM frontend, let wasm_bindgen generate JS bindings then use additional tooling to convert that into Dart bindings. If we want to reuse code, we would have to provide a web implementation of SendPorts since isolates do not exist on the web, which AFAIK has not seen any significant progress yet. web_ffi seems fascinating to me, but it only works with Emscripten so we wouldn't benefit from the wasm_bindgen ecosystem, and I think we're better off not reinventing the wheel here.

@Desdaemon Sounds quite reasonable and agree with you!

Then we may need to firstly have a refactor of the current codebase in order to support multiple frontends. We may refer to other Rust code generators to see how they structure the code. This open source project has grown bigger and more mature as time goes by (of course, with much contribution from you).

I've been doing some work to add wasm_bindgen as a backend for web_ffi as well as substituting SendPort and ReceivePort with a pure Dart implementation on web. There is still a lot of work and investigation to be done, and since this project spans all three repos it might take some time to get things moving. Regardless, I'm quite optimistic of how this might turn out.

@Desdaemon Sounds great! Looking forward eagerly to your progress!

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Stale not stale

I just found https://gpalma.pt/blog/flutter-web-web-assembly/ which is pretty much the exact manual steps I was considering to get the mandlebrot set tutorial working on the web. It seems like it might side step any issues with package:web_ffi.

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.