Coder Social home page Coder Social logo

javascriptbert's Introduction

BERT-JS

For Other Languages

What is BERT?

BERT (Binary Erlang Term) is a format created by the Erlang development team for serializing Erlang terms, and promoted by Tom Preston-Werner as a way for different languages to communicate in a simple and efficient manner.

Erlang External Term Format

What is BERT JS?

BERT-JS is a first cut Javascript implementation of the BERT protocol. In other words, using BERT-JS, you can serialize data into a binary format that can then be de-serialized by Erlang directly into an Erlang term.

Limitations

  • Decoding floats is not yet supported.

Usage With WebSockets

SetUp Bert

    function setUpBert() {
        bert = new BertClass();
        bert.encodeObjectKeysAsNumber = true;
        bert.encodeStringAsBinary = true;


        //decodes erlang map binary value as String for 'key'
        bert.markDecodeBinaryAsStringForKey(key);
    }

    var WS = false;
    if (window.WebSocket) WS = WebSocket;
    if (!WS && window.MozWebSocket) WS = MozWebSocket;

    if (!WS) { return; }

    var websocket = new WS(serverUrl);
    websocket.binaryType = "arraybuffer";
    websocket.onmessage = onMessage;

Receive Json

    function onMessage(msg) {

        if (typeof msg.data == "string") {
            console.log("received data: ", msg.data);
            return;
        }

        var array = new Uint8Array(msg.data);
        var charData = bert.bytes_to_string(array);
        var json = bert.decode(charData);

        console.log("decoded", json);
    }

Send Json

    //json = {}; //json is object

    function send(json) {
        var encoded = bert.encode(Obj);
        var byteArray = bert.binary_to_list(encoded);
        websocket.send(new Uint8Array(byteArray));
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.