Skip to content

realtime: support async encode/decode for off-isolate (de)serialization #1401

Description

@spydon

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    realtimeThis issue or pull request is related to realtimev3

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions