0

I am working on a web portal where In my scenario there are big size JSON strings (1MB) which are sent by the server. We are using the WebSocket protocol. Obviously, it's taking a long time to load on client's browser.

I have tried Gzipping the JSON at the server end and try to decompress using Javascript. Compression is fine but I get some error while decompressing using JS. I used this library http://jsxgraph.uni-bayreuth.de/wp/2009/09/29/jsxcompressor-zlib-compressed-javascript-code/

Could anyone please suggest if there is another way?

P.S. As I am using WebSocket so I can't rely on browser's decompression.

Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43
Elf
  • 659
  • 9
  • 19
  • Do you always see the error when decompressing? Or is it intermittent? If you're using gzip to compress on the server and zlib to decompress on the client (browser/JavaScript), that would be a problem. While the 2 are similar, they're not bitstream compatible. – Multimedia Mike Nov 28 '13 at 06:08
  • I get error all the time. Even I doubt that Gzip and Zlib combination will work. Do you have any other idea/solution for this? Thanks for your reply! – Elf Nov 28 '13 at 07:27
  • How are you compressing the data on the server side? Dynamically through PHP? Some other language? What function are you calling to do the compression? – Multimedia Mike Nov 28 '13 at 17:09
  • I am compressing in JAVA. I used the ByteArray and GzipOutputStream combination as per this post: http://stackoverflow.com/questions/16351668/compression-and-decompression-of-string-data-in-java – Elf Nov 29 '13 at 04:16

2 Answers2

1

Thanks for responding with clarifications. I think I see the problem now. Overall, remember that zlib != gzip, not exactly. Generally, gzip is zlib data but with some extra headers. JSXCompressor wants zlib data but you're sending gzip data from Java. To explain further:

On the page you linked to which describes JSXCompressor, the example server side code uses PHP's gzcompress function. From the documentation: "This function compress the given string using the ZLIB data format." (Emphasis theirs.) The documentation further advises using gzencode if gzip is truly desired (it's not, in this case).

Here's the documentation for Java's GZIPOutputStream class. Note that this class outputs data in gzip format. It looks like the solution is to climb one level higher in the class hierarchy and leverage the DeflaterOutputStream class.

Multimedia Mike
  • 12,660
  • 5
  • 46
  • 62
  • Thanks Mike! I changed the server side compression to use DeflaterOutputStream but still I am getting error while decompressing. I checked js and it is using some function such as .length, charAt() etc on the zipped content and that is why it is throwing error. The js file is min.js and I work mostly at server side so I do not know what is really wrong in js file. – Elf Dec 03 '13 at 06:11
0

I was not encoding(BAse64) the string after Gzipping it and that is why the js was complaining. After doing Base64 encoding its working fine even with GZIPOutputStream at Java end. I am using non-minimized version of the jsxcompressor from here:

http://sourceforge.net/apps/trac/jsxgraph/export/2808/trunk/JSXCompressor/

Elf
  • 659
  • 9
  • 19