Skip to content

Serialization Issues

Prefer Using Native Interfaces for Serialization

The channel module does not handle message serialization. When calling the commit method, the information passed to the channel instance's postMessage method is a JS object containing the call stack.

If you expect the native communication interfaces of different JS runtime environments to have implemented the structured clone algorithm, you should prefer using the native communication interface as the channel. This way, in most cases, you don't need to worry too much about serialization issues.

TIP

If the message sending method of the native communication interface is named postMessage, it is likely that it already supports the structured clone algorithm.

However, in some scenarios, you may still face serialization issues, including:

  • Scenarios where functions need to be serialized.
  • Scenarios like WebSocket that only support sending and receiving string messages.

Here is a set of JSON-based serialization solutions:

LibraryFunctionality
json-serializationAn asynchronous JSON serialization library that automatically handles circular references and supports custom serialization rules.
@json-serialization/binaryProvides binary serialization and deserialization mechanisms, supporting data types like ArrayBuffer, Buffer, Blob, File, Uint8Array.
@json-serialization/errorProvides a serialization and deserialization mechanism for Error and its subclasses.
@json-serialization/functionProvides function serialization and deserialization mechanisms, enabling cross-context function calls without using eval.

These libraries are not only suitable for the channel module but can also be used in other scenarios.

Installation and Usage Example

ts
<!--@include: ../../snippets/guide/install.sh-->

The example code demonstrates how to wrap a basic channel that only supports sending and receiving string messages into a channel that supports sending and receiving more types of messages.

ts
<!--@include: ../../snippets/guide/use.ts-->