Background
In #1397 the realtime client gained a pluggable encode/decode codec (RealtimeEncode / RealtimeDecode), defaulting to the protocol-2.0.0 serializer. The signatures are synchronous:
RealtimeEncode = Object Function(Map<String, dynamic>)
RealtimeDecode = Map<String, dynamic> Function(Object)
Because they're synchronous, jsonEncode/jsonDecode of the message payload run on the main isolate. For large payloads this blocks the event loop (and can cause UI jank on Flutter), and a consumer cannot plug in a background-isolate codec such as yet_another_json_isolate (whose encode/decode are async).
Proposal
Allow asynchronous encode/decode so (de)serialization can be offloaded to a background isolate:
- Make
RealtimeEncode / RealtimeDecode (or an alternative codec object) return Futures, e.g. Future<Object> Function(Map<String, dynamic>) / Future<Map<String, dynamic>> Function(Object).
- Update
RealtimeClient.push to await the encode before writing to the sink, preserving send ordering.
- Update the incoming-message flow (
onConnectionMessage) to handle an async decode without reordering frames.
- Keep the default synchronous serializer fast-pathed (avoid unnecessary microtask hops when no custom codec is supplied).
Notes
- This is a breaking change to the codec typedefs, so it is targeted at v3.
- Send/receive ordering is the main risk to get right when introducing awaits into the message pipeline.
Tracked for v3 (#1278).
Background
In #1397 the realtime client gained a pluggable
encode/decodecodec (RealtimeEncode/RealtimeDecode), defaulting to the protocol-2.0.0 serializer. The signatures are synchronous:RealtimeEncode=Object Function(Map<String, dynamic>)RealtimeDecode=Map<String, dynamic> Function(Object)Because they're synchronous,
jsonEncode/jsonDecodeof the message payload run on the main isolate. For large payloads this blocks the event loop (and can cause UI jank on Flutter), and a consumer cannot plug in a background-isolate codec such asyet_another_json_isolate(whose encode/decode areasync).Proposal
Allow asynchronous encode/decode so (de)serialization can be offloaded to a background isolate:
RealtimeEncode/RealtimeDecode(or an alternative codec object) returnFutures, e.g.Future<Object> Function(Map<String, dynamic>)/Future<Map<String, dynamic>> Function(Object).RealtimeClient.pushto await the encode before writing to the sink, preserving send ordering.onConnectionMessage) to handle an async decode without reordering frames.Notes
Tracked for v3 (#1278).