cross_isolate_messenger 0.0.1
cross_isolate_messenger: ^0.0.1 copied to clipboard
A lightweight utility for message passing between Dart isolates with persistent storage using SharedPreferences. Designed for native Flutter apps.
๐จ Cross Isolate Messenger #
Cross Isolate Messenger is a lightweight Dart utility for message passing and persistence across multiple Dart isolates in Flutter applications.
It uses SharedPreferences under the hood for durability, making it ideal for native platforms (Android, iOS, macOS) where isolates might need to resume processing messages after being restarted or delayed.
โจ Features #
- ๐ Message persistence: Messages are stored in
SharedPreferencesuntil acknowledged. - ๐ฆ Isolate communication: Broadcast messages across Dart isolates using named channels.
- ๐ง LRU cache-based deduplication: Prevents duplicate processing across restarts or replay.
- ๐งฑ Generic and extensible: Supports custom classes via decoders and multiple message streams via unique
portNames.
๐งฉ Requirements #
โ Native platforms only โ This package relies on
SharedPreferences, so it will not function on web or unsupported platforms like Windows or Linux (withoutSharedPreferencessupport).
๐ Usage #
1. Create an instance (per channel) #
final queue = CrossIsolateQueue<Map<String, dynamic>>.getInstance('chat_channel');
await queue.initialize();
queue.stream.listen((msg) {
print('Received message: $msg');
});
2. Send a message from another isolate #
await CrossIsolateQueue.send('chat_channel', {
'id': 'message-001',
'content': 'Hello from another isolate!',
});
3. Acknowledge a message #
await queue.ack('message-001');
๐ Custom Message Types #
Define a class and a decoder:
class MyMessage {
final String id;
final String content;
MyMessage(this.id, this.content);
factory MyMessage.fromJson(Map<String, dynamic> json) =>
MyMessage(json['id'], json['content']);
}
Use the decoder when creating the queue:
final customQueue = CrossIsolateQueue<MyMessage>.getInstance(
'custom_channel',
decoder: (json) => MyMessage.fromJson(json),
);
await customQueue.initialize();
๐งผ Clean Up #
await queue.dispose();
โ Limitations #
- โ Not supported on web or desktop platforms without
SharedPreferences.
๐ Related #
๐ License #
MIT
๐ท TODO #
- โ Add fallback for unsupported platforms
- โ Add testing harness for isolate message passing