GithubHelp home page GithubHelp logo

5ht / binary_ws Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zookzook/binary_ws

0.0 1.0 0.0 2.53 MB

Phoenix live view + binary websocket

License: MIT License

Elixir 51.79% CSS 24.27% JavaScript 22.53% HTML 1.41%

binary_ws's Introduction

BinaryWs

This is a demo application for Phoenix LiveView that uses BERT (Binary ERlang Term) serialization instead of JSON serialization for server-client communication.

The basic idea is to use :erlang.term_to_binary/1 and :erlang.binary_to_term/2, so that the serialization is done as fast as possible. The browser/client uses a BERT serializer in JavaScript instead of the JSON.stringify() and the JSON.parse() function. This is not as fast as the JSON serialization, but the server can use optimized functions (written in C) now. This reduces the load on the server and the client has to work a little more. As a result, the WebSocket message is processed faster and the server can handle more push events.

What is to be done?

The client needs to configure the BERT serializer:

import * as bs from "./bert-serializer"

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, encode: bs.encode, decode: bs.decode });

liveSocket.connect();

the BERT-Serializer encodes only the data types known in JSON:

  • Strings
  • Numbers (Integer, Floats)
  • Arrays
  • Maps
  • null, undefined

Other data types such as tuples are not expected.

The serialization for the websockets is configured at the 'endpoint' in case of the server:

  socket "/live", Phoenix.LiveView.Socket,
         websocket: [
           connect_info: [session: @session_options],
           serializer:  [{BERTSerializer, "~> 2.0.0"}]
         ]

After the restart you should see the following message:

[info] CONNECTED TO Phoenix.LiveView.Socket in 86µs
  Transport: :websocket
  Serializer: BERTSerializer

Benchmark

Simple benchmarks have shown that encoding and decoding are 2 to 10 times faster. The bigger the push message, the greater the factor.

I have used the rainbow demo as an example. For this, 10 tabs were opened for each configuration (JSON vs BERT) and the FPS was set to 100.

JSON-Serializer:

  • average 60MB memory usage
  • output: about 2.4 MB
  • scheduler utilization: 20-30 %

JSON-Serializer

Full-BERT-Serializer:

  • Memory usage: about 54 MB
  • Output: about 2.8 MB Output
  • Scheduler utilization: 15-20 %

BERT-Serializer

Security Concerns

The BERT format enables to encode anonymous functions and others, which can open up the server for remote code execution. To avoid this the BERT serialization is only used from server to client. The client sends JSON as usual. The server sends TERM instead of JSON. Running the same benchmark but now only used TERM from server to client:

Half-BERT-Serializer:

  • Memory usage: about 56 MB
  • Output: about 2.8 MB Output
  • Scheduler utilization: 15-20 %

HALF-BERT-Serializer

We got nearly the same results as before.

Sizes

The factor between the sizes of JSON and TERM depends on the content. If the content contains a lot of characters which need to be escapted then the factor byte_size(TERM)/byte_size(JSON) is < 1. With HTML you get a lot of " which need to be escaped, so the factor is usually <= 1.

Credits

The JavaScript for encoding and decoding BERT is taken from https://github.com/synrc/n2o/blob/master/priv/bert.js and has been slightly modified.

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.