folkwaysDocs
JavaScript SDK

Serialization and codecs

Encode payloads, consume raw bytes and use external application codecs.

See Payloads and schemas for the platform model. The JavaScript SDK accepts ordinary objects, strings, ArrayBuffer and typed-array views. Without an explicit content type, bytes remain Bytes, strings become Text, and other JSON-serializable values become JSON.

TypeScript
import { encodePayload, decodePayload } from '@folkwaysio/realtime';

const encoded = encodePayload({ reading: 42 });
const decoded = decodePayload(encoded.contentType, encoded.payload);
console.log(decoded);

const binary = encodePayload(new Uint8Array([1, 2, 3]), 'flatbuffers');
console.log(binary.contentType, binary.payload.byteLength);

The binary array above illustrates labeling only; it is not a valid FlatBuffers table.

With explicit json, a string is taken as already serialized JSON; objects are serialized. Explicit text accepts a string or bytes. Other content types require bytes. JSON BigInt values are converted to decimal strings; unsupported top-level values and circular objects fail serialization. If received JSON cannot be parsed, the SDK returns its raw bytes rather than a validated object. Text is UTF-8 decoded. Other content types return bytes.

Use message.contentType and message.payload for binary decoding, and treat raw payload arrays as read-only. External encoders and runtime validators remain application responsibilities. See FlatBuffers, Protobuf and schema validation.