flutter_rust_bridge icon indicating copy to clipboard operation
flutter_rust_bridge copied to clipboard

EventChannel support

Open lzunsec opened this issue 2 years ago • 12 comments

I want to pass data from Rust to Flutter in a stream way. That is, I don't want Flutter do call Rust to get an answer, but Rust could call and send stuff to Flutter at any time.

There's a solution for that in Java, called EventChannel, like here: https://github.com/seamusv/event_channel_sample/blob/master/android/app/src/main/java/com/yourcompany/event_channel_sample/MainActivity.java#L28

Would be nice to have them directly on Rust!

lzunsec avatar Jun 28 '22 15:06 lzunsec

Hi! Thanks for opening your first issue here! :smile:

welcome[bot] avatar Jun 28 '22 15:06 welcome[bot]

There are a few approaches:

  1. To reuse current codebase, maybe let Flutter call Rust and open a "stream", and later rust can put things into this stream and flutter will receive it. This is what I have done for my own app for "let rust send logging data to flutter".
  2. You may modify flutter_rust_bridge, use the fact that Rust can send data to Dart via IntoDart (and the underlying dart_native_api)

Feel free to make a PR!

fzyzcjy avatar Jun 28 '22 22:06 fzyzcjy

Well I looked at EventChannel example. It is just like approach 1, the stream.

fzyzcjy avatar Jun 28 '22 22:06 fzyzcjy

Is it possible to open a never ending stream from flutter into rust such that rust can always send things back? This would be nice because the EventChannel thing I listed requires rust to call java to call flutter, so it's platform specific and bad.

lzunsec avatar Jun 28 '22 23:06 lzunsec

Is it possible to open a never ending stream from flutter into rust such that rust can always send things back?

Yes. For my personal usage (the logging), I do not end the stream

fzyzcjy avatar Jun 29 '22 00:06 fzyzcjy

On the docs it says

pub struct LogEntry {
    pub time_millis: i64,
    pub level: i32,
    pub tag: String,
    pub msg: String,
}

// Simplified just for demonstration.
// To compile, you need a OnceCell, or Mutex, or RwLock
// Also see https://github.com/fzyzcjy/flutter_rust_bridge/issues/398
lazy_static! { static ref log_stream_sink: StreamSink<LogEntry>; }

pub fn create_log_stream(s: StreamSink<LogEntry>) {
    stream_sink = s;
}

I think it should be

pub fn create_log_stream(s: StreamSink<LogEntry>) {
    log_stream_sink = s;
}

right?

Anyways, I'm getting

the trait bound DartCObject: From<LogEntry> is not satisfied

even after codegen.

Also I think it would be nice to mention

use flutter_rust_bridge::{StreamSink};

cau I thouoght it was a Rust thing, not a type from this lib

lzunsec avatar Jun 29 '22 00:06 lzunsec

right?

Sure

fzyzcjy avatar Jun 29 '22 00:06 fzyzcjy

even after codegen.

Please file a separate issue about this

Also I think it would be nice to mention

Sure, feel free to PR

fzyzcjy avatar Jun 29 '22 00:06 fzyzcjy

Hmmm I'm using https://github.com/fzyzcjy/flutter_rust_bridge/pull/293 I think this might be the reason of the errors, cause it's old. Now I don't know what to do, cause I need it working but also need StreamSink

lzunsec avatar Jun 29 '22 00:06 lzunsec

https://github.com/fzyzcjy/flutter_rust_bridge/pull/537

lzunsec avatar Jun 29 '22 01:06 lzunsec

Sorry my type was not pub so it wasn't being translated. Anyways, do you think I should have many streams per instance or just one stream and filter the messages? I don't know how expensive this stream is.

Thank you for your help!

lzunsec avatar Jun 29 '22 01:06 lzunsec

Sorry my type was not pub so it wasn't being translated

Feel free to PR and warn (or log) such cases

Anyways, do you think I should have many streams per instance or just one stream and filter the messages? I don't know how expensive this stream is.

IMHO it is not expensive, so it should not be problem if only dozens of streams. But if you have a million stream maybe too much

fzyzcjy avatar Jun 29 '22 01:06 fzyzcjy

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[bot] avatar Aug 31 '22 00:08 stale[bot]

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.

github-actions[bot] avatar Sep 21 '22 01:09 github-actions[bot]