array-buffer-serializer
v0.4.2
Published
Serializes data to transfer fewer bytes over the network
Downloads
13
Maintainers
Readme
array-buffer-serializer
Similar (without fixed length data representation and some other minor differences) as CBOR or MessagePack, allows to encode an object into bytes before transmission via WebRTC or WebSocket and decode it back when received. This is very useful for network bandwidth when you need to send data to one or more recipients frequently. Works in both: browser and node.
Some characters of the Latin-1 Supplement are reserved for encoding values: C0 (U+00C0) - E3 (U+00E7). Supports only ASCII string.
Installation
Using npm:
npm install array-buffer-serializer
With yarn:
yarn add array-buffer-serializer
Usage
Import Serializer object
// as CommonJS const Serializer = require("array-buffer-serializer"); // or ES6 import Serializer from "array-buffer-serializer";
Encode some data (object or array) using "toBuffer" method
// as map const data = {this: "way"}; // or array const data = ["or", "that", "way"]; const buffer = Serializer.toBuffer(data);
Send the buffer (for example, over WebScoket)
ws.send(buffer);
Receive the buffer and decode it using "fromBuffer" method
ws.addEventListener("message", buffer => { const data = Serializer.fromBuffer(buffer); });