Firebase messaging's spawned isolate and main app – how to pass data(Web)?

Issue

According to https://firebase.flutter.dev/docs/messaging/usage/
onBackgroundMessage(…) is running on its own isolate.

How do I pass data from that isolate (which automatically spawns and runs a top level function) to the main app without using persistent storage (sqlite/files) which isn’t supported on the web platform?

Solution

Flutter Web does not support isolates. You are likely looking in the wrong documentation tab. Still, what you are looking for may just not be supported yet.

Web requires you to register a JavaScript Service Worker which runs in the background.

Unfortunately we haven’t yet been able to establish a proper way of communicating with the Service Worker and Flutter applications. Right now, all web background code must be executed in the JavaScript Service Worker file.

EDIT:

In order to share data between WebWorkers and the app, you can try using the awesome Drift library (formerly Moor).

You can offload the database to a background thread by using Web Workers. Drift also supports shared workers, which allows you to seamlessly synchronize query-streams and updates across multiple tabs!

You should be able to communicate with the background database web worker and save your data. If correctly setup, this will in turn update your frontend.

Maybe checkout this example: https://github.com/simolus3/moor/tree/develop/extras/web_worker_example

Answered By – kuhnroyal

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *