Repository: flutter
Issue: [Discussion] A communication channel between an isolate and platform code on an arbitrary thread
Labels: engine,customer: google,c: proposal,P3,a: plugins,team-engine,triaged-engine

Filing this to start and track a discussion.

Currently the only mechanism that Flutter plugins can use for communication between the Dart code and the platform specific code on Android and iOS are platform channels.
Platform channels enforce a single cross-thread flow: messages from the main Dart isolate(running on the UI thread) are sent to the platform specific code running on the platform thread.

Allowing passing messages from Dart code running in a different isolate to platform-specific code(e.g Java on Android) running not on the platform thread provides some needed flexibility, specifically I have 2 main concrete scenarios in mind:

### Streaming data from Dart to platform code
#27896 is a request for allowing the Dart code to provide a byte stream for data to be loaded by a webview. The Android API lets us provide a Java Stream to the webview, and the webview will read this stream on a background thread.

What we need here is a stream that is fed by Dart code on one side, and is read by the Java code on the other side, implementing such a stream over a platform channel enforces involving the UI thread and the platform thread in the dance (UI_THREAD->PLATFORM_THREAD->BACKGROUND_THREAD) for every data chunk.

A communication channel between a Dart isolate and platform code on an arbitrary thread(or maybe on the thread running the Dart isolate) will allow doing so without involving the UI and platform threads.

Note that I did not run benchmarks and don't have a good feeling for how significant the UI performance gain would be for such a thing(I guess going through the UI and platform threads might not be that bad as the Dart code won't be blocking on IO but would just occasionally send some data to the platform thread which will just have to notify the background thread).
I wonder if others have a better hunch than me for the performance cost involved with going through the UI and platform threads.

### Making "synchronous" calls from platform code to Dart code
This came up most recently in #25329 where we need to provide a Java API with a callback that has to make a synchronous decision(and this decision makes most sense to be made by Dart code). For #25329 we came up with a pretty complex workaround to avoid the need for a synchronous decision(that has drawbacks, specifically giving up on some webview security mechanisms).
This has also came up in other use-cases where we wanted to use platform APIs that requires being handed with callbacks that makes synchronous decisions.

While we can devise a simpler mechanism for allowing synchronous calls(@jason-simmons and I have discussed a few options), we should discourage users from using it unless they really know what they are doing(generally you shouldn't block the platform thread), and what better way is there other than making it hard to do :)

Specifically if we had a communication channel from Dart to Java code running not on the platform thread (whether through a separate isolate or not), we could block the platform thread and unblock it by sending a message from Dart to that other thread that will unblock the platform thread.

Happy to hear your thoughts, and ideally decide whether this is something worth doing or that the benefits are not important enough.
cc @chinmaygarde @jason-simmons @cbracken