views:

303

answers:

2

I transfer a fair amount of XML from the server to the client, in my application (250K-500K each time).

How can I compress it on the server, and decompress it on the client using standard JavaScript? Is it possible?

+1  A: 

Is this HTTP type transfers?

The easiest way would be to look into compression at the web server side.

XML Usually compresses really well.

Adrian
HTTPS transfers, yes. I do have web server compression enabled. I suppose this is the best I can do?
Jason
+2  A: 

First, a scolding and possible workarounds. Second, a possible crazy solution.

The Scolding

If you're sending 250k - 500k back and forth between the client and server, you're doing it wrong. Look into

  1. Only requesting and sending back the information you need

  2. If there's information in the XML that doesn't change often, configure your web-server for aggressive http caching for these requests. Also, consider sending the "doesn't change often information" down with the initial page request instead of grabbing it from the server

  3. Consider sending down JSON instead of XML. JSON is less verbose than XML and will usually take up less space. If your client-side code NEEDS an XML object, reconstruct it from the JSON you just downloaded.

  4. If you're using apache, mod_deflate will gzip your data for client that can accept it. Last time I looked into gzip encoding on IIS it did it's zipping and unzipping on disk, which quickly became a bottle-neck in high load situations (YMMV)

The Crazy Solution

If you're hell-bent on trying the compress-in-javascript route, the LZ77 compression algorithum has been ported to most languages, including javascript.

Find the analogous code for your server platform, compress the XML, and then feed it to the code found in the link above. To avoid encoding issues, I'd try wrapping the compressed data itself in either XML or JSON when sending it down.

Javascript compression performance is going to be slow.as.molasses, which is why I discourage this route.

Alan Storm
I've had situations where I need to send large amounts of data back and forward. For instance - real example here - stats about number of sales of products every hour for a week for a store.I"m not using JS, but it is XML data (until I rewrite in JSON, which I really want to do!)
Matthew Schinckel
Thanks for the scolding. The data does change (and I do need it all)... maybe JSON would be a better solution because it is smaller. I will look into that, thank you.
Jason