Hi, I want to write a Flutter app that spawns a process (written in Rust), and somehow have communication between the Flutter app and the Rust Process. I am searching for a reasonable design to do something like this.
Prior research
I checked around. Flutter's website contains some information about platform channels and platform specific code:
The example in the page above contains information about how to invoke platform specific functions (For example, using Kotlin on Android). However, the interaction is something like one function invocation per message, which is not what I want to achieve.
Another page I read is from this blog:
which contains useful information about how to call Rust code from Kotlin (on android) or from Swift (on ios). The Rust code in this example is being used as a library.
All the examples I managed to find show how to call one Rust function, but I couldn't find a design example for running a Rust process in the background and communicating with it.
Information about the project
I am working on Offst, it's a decentralized credit card (https://www.offst.org). I plan to port it to run on mobile phones (Android and ios). Offst is peer to peer system, and every device should run its own node instance. A node is practically one process that does the main logic and communication. It should stay running in the background. It has two channels of communication:
- Communication facing the user, allowing the user to do stuff.
- Communication against the outside world (As this is a peer to peer system).
Communication facing the outside world is easy, it happens with sockets. Communication facing the user is a bit more difficult for me, because it needs to interface with the Flutter app somehow and I'm not sure how to do this.
There are some things I know for sure I want to be able to do:
- Trigger spawning a new node process (Rust code) from the Flutter app.
- Have secure communication between the Flutter app and the newly spawned node process.
- Be able to track all the spawned Rust processes.
Side note: I know that blockchain related projects are sometimes frowned upon in the community, so I want to say upfront that Offst doesn't contain any blockchains, proof of work, proof of stake or anything related. It's something else: Offst is more like splitwise, but decentralized.
Solution ideas
-
Create a version of the node (a Rust process) that communicates with the user through stdin/stdout. Then use dart code in the Flutter app to spawn a process. I have seen some examples here: Process class - dart:io library - Dart API . If I go with this approach I will somehow need to figure out from the dart code which node binary to call (Android or ios), and I'm not sure if it is considered good form to do this from the dart codebase.
-
Create a version of the node that listens on a local socket, and let the Flutter app connect to the socket. My feeling is that something is insecure about this approach.
-
Create a library version of the node (Like done on most examples I could find on the web), and call the library from kotlin code (in android) or swift code (in swift). The library code will somehow spawn a process. I'm not fully sure if this is possible to do, and if so, can this be done safely?
I also heard that dart has new experimental ffi interface, I am willing to live on the edge if it can help me solve this problem.
Note about security: By secure I mean a solution that will not allow other entities on the mobile phone to hijack the communication between the Flutter app and the process. The communication is already encrypted and signed properly, but in any case I would like to enforce the security of the communication channel on the system level too.
Any ideas about how to design a solution are welcome. I would also be happy to get criticism for any of the ideas above!