Skip to content

Bidirectional communication #29

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

Closed
ostafen opened this issue Nov 25, 2021 · 6 comments
Closed

Bidirectional communication #29

ostafen opened this issue Nov 25, 2021 · 6 comments

Comments

@ostafen
Copy link

ostafen commented Nov 25, 2021

Hi, first of all thanks for the work.
It is not clear to me how I could implement bidirectional communication between the UI and the foreground service isolates.
Currently, it seems that, if the UI wants to send data to the service, it can only use the saveData function.
Moreover, how can I recover the receivePort between app restarts withour restarting the service?

Thank you

@Dev-hwang
Copy link
Owner

Dev-hwang commented Nov 26, 2021

Currently, the only way to send data from main isolate to background isolate is to use the saveData function.

I've been working on an implementation for a long time to enable bidirectional communication. However, in the process, the ReceivePort object was lost while the service was running, or data could not be delivered properly. I'm continuing to check and test this issue. If there is a good way, I would be grateful if you could provide a PR.

And since the ReceivePort object returned when the service starts is created in the main isolate, there is no way to recover it when the app is restarted (A new main isolation seems to be created when the app is restarted). So I adopted the method of creating a new ReceivePort using restartService function.

If you want to share ReceivePort on all pages without restarting the service, check this issues page.

@ostafen
Copy link
Author

ostafen commented Nov 26, 2021

There is no way to persist the ReceivePort on disk in order to recover it after app restart?

@ostafen
Copy link
Author

ostafen commented Nov 26, 2021

Alternatively, Is there a way to prevent the app from being closed when the foreground service is running?
If i press the back button the app is minimized, but if I close it, then it restarts

@Dev-hwang
Copy link
Owner

Dev-hwang commented Nov 29, 2021

By using this plugin, the app can have a screen(Activity) and a service(ForegroundService). Currently, this plugin's ReceivePort is used for communication between the service and the screen. Importantly, when the app is completely closed, the screen is destroyed by the screen's life cycle. At this time, the ReceivePort object that was communicating with the service is also destroyed. Because the screen was subscribing to ReceivePort. So, when the app is restarted, you need to create a new ReceivePort so that it can communicate with the service.

It is the correct behavior for the service to restart as the app needs to create a new ReceivePort when it restarts.

@ostafen
Copy link
Author

ostafen commented Dec 4, 2021

Thank you for your reply. For my project, I can't get rid of bidirectional communication. Unfortunately, it is difficult to adapt this plugin because of how it is designed. Moreover, there is no currently plugin in the web allowing for bidirectional communication in foreground service. Bidirectional communication can be achieved simply by using method channels calls, getting rid of any concept related to dart isolates. Also the fact that the library is meant to run repeatedly a given task represents a serious limitation for my project (why don't let the developer the freedom to run a Timer inside its dart code on the service?).
So I decided to write my own plugin: https://github.com/ostafen/flutter_foreground_worker. It is still under development and at the early stages, but it does the job (currently, only for android).
The core idea is the abstraction of ForegroundWorker, an entity which can receive and send messages.
If you are interested, we could collaborate in order to find the best approach, and unificate all inside a library.

@nuc134r
Copy link

nuc134r commented Jul 26, 2023

I think I have found a way to have a reliable bi-directional communication. See #44 (comment)

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

No branches or pull requests

3 participants