5

Greerings all

Is there a way to compress data sent from php (server) and then uncompress the data using javascript (client)?

Thanking you

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Imran Omar Bukhsh
  • 7,849
  • 12
  • 59
  • 81
  • 3
    You could `gzip` the response and let the browser decompress it (that works at the HTTP protocol level). Or is there a specific reason you want to decompress it with JS? Afaik you would have to implement the logic on your own (or find some third party library of course). – Felix Kling Jul 03 '11 at 08:19

3 Answers3

8

I have to agree with @Domenic's answer here. @Nishchay Sharma is way off.

The only thing I'll add is if you want to do this on a per-script basis rather than configuring your entire server to compress everything, it's trivial to accomplish your goal by using PHP's gzencode() function coupled with a header call:

http://www.php.net/manual/en/function.gzencode.php

For instance, let's say you are retrieving a huge set of data via an Ajax call to a PHP page. You could configure the PHP page to use gzencode as follows:

<?php
$someBigString = gzencode('blahblah...blah');

header("Content-type: text/javascript");
header('Content-Encoding: gzip'); 

echo $someBigString;  
?>

(This is overly simplified, of course, but I'm keeping it simple.)

Your JS Ajax call will pull the data down, see the gzip header, and decompress it automagically. I personally use this technique for very large geo-coordinate data sets for Google Maps that can be many megabytes in size when uncompressed. It couldn't be easier!

sstringer
  • 486
  • 7
  • 12
2

Yes; if you configure your server to serve up the data, which hopefully you are sending in a sane format like JSON, using GZIP compression, then you can just do an Ajax call in JavaScript and it will be automatically decompressed by the browser.

To set this up, copy these lines into your .htaccess file. (I assume you're using Apache, since that is the most common platform for serving PHP.)

Domenic
  • 110,262
  • 41
  • 219
  • 271
  • this is equivalent to compressing and decompressing both in php. after all ajax call will in turn execute php only. – Nishchay Sharma Jul 03 '11 at 08:42
  • 2
    How is this equivalent to compressing and decompressing both in PHP? The decompression is done on the client side---by the browser, not JavaScript, but certainly not by PHP. – Domenic Jul 03 '11 at 08:44
  • Browser just asks the server to call a php function (or asp/jsp maybe). AJAX call starts on the client side by javascript but ends at the server side only. – Nishchay Sharma Jul 04 '11 at 06:52
  • 2
    @Nishchay Sharma that is not how GZIP compression works; I suggest you look at the Chrome or Firefox source code if you want proof. That would be highly inefficient and defeat the entire purpose of GZIP. – Domenic Jul 04 '11 at 06:57
  • 1
    Hmm. you are right. so dumb of me. :P After reading your answer carefully, i get the point :) – Nishchay Sharma Jul 04 '11 at 07:25
  • your link is broken. can you please keep those line in the ANSWER? –  Feb 24 '17 at 21:32
1

If keeping your response overhead small as possible is your goal then JSON DB: a compressed JSON format might also be of interest to you.

Sebas
  • 21,192
  • 9
  • 55
  • 109
Jay Sidri
  • 6,271
  • 3
  • 43
  • 62