0

This question is a bit off from what is considered a good Stackoverflow question, but still, I guess this is something many people may have needed, so Here's my situation:

I'm making a web application that will be mainly accessed through mobiles, on places that have very slow internet uplink (farms), and yet it will need to keep a very dynamic information exchange with the server.

So far I'm using a websocket to reduce the communications overhead, exchanging only raw data on JSON arrays and even receiving the css and scripts through this socket, all minifyed.

In order to reduce even more the data exchange, I thought about using GZIP compression on the server side, and decompressing all on the client side, and vice-versa.

Being a complete noob in the compression universe, I ask you dear fellows if are there any javascript plugin for compression/decompression of text data?

Something that doesn't take a life to load on first access (to be specific, less than 1M). Below is my unminifyed initial script, that handles all the communication:

let ws = new WebSocket("wss://localhost/Socket");

ws.onopen = function () {
    let
            functions = new Map(),
            loadedScripts = new Set();

    //receives a JSON array with two elements
    //the first tells what to do with the data,
    //which is an integer that is key for a mapped function
    ws.onmessage = function (event) {
        let data = JSON.parse(event.data);

        //finds the determined function on the map and passes the second
        //item of the array as argument to it
        functions.get(data[0])(data[1]);
    };

    // the function #1 is the one who discovers if the reqested script
    // and version are already loaded on are stored on the local storage
    // from a previous access, if not sends a request for the given script
    // it receives an array of "requirements"
    functions.set(1, function (array) { //require [name, token, version]
        for (let require in array) {
            loadedScripts.add(require[0]);
            if (typeof window[require[0]] !== "function" || eval(require[0]).version !== require[2]) {
                if (eval(localStorage[require[0]]) !== require[2]) {
                    ws.send(JSON.stringify([2, require[1]]));
                }
            }
       }
    });

    // the #2 function is the one who evaluates the string, which is a 
    // script, received on the websocket
    // HERE COMPRESSION WOULD BE VERY HELPFULL
    functions.set(2, function (script) { //[scriptName, script]
        eval(script[1]);
        try {
            localStorage[script[0]] = script[1];
        } catch (e) {
            if (e instanceof DOMException) {
                for (let k in localStorage) {
                    if (!loadedScripts.has(k)) {
                        localStorage.removeItem(k);
                    }
                }
                functions.get(2)(script);
            } else {
                throw e;
            }
        }
    });
};

//...

I apologize in advance for any misspelling or bad text, not my language.

EDIT

I have actually googled around for some plugins for that, but my very limmited knoweldge about this topic made me not understand most of it. So my question a little more of a "compression for noobs" kind.

Lucas Noetzold
  • 1,670
  • 1
  • 13
  • 29
  • [this other off topic question](https://stackoverflow.com/questions/4875020/javascript-decompress-inflate-unzip-ungzip-strings) has many possible solutions to your off topic question :p – Jaromanda X Jul 16 '18 at 02:52
  • Yep, duplicate. I didn't even knew the terminology to get there. Thanks. – Lucas Noetzold Jul 16 '18 at 02:57

0 Answers0