This post consist of two parts which will let you know how to send large amount of data by compressing it at client side and decompress it at server side using LZ algorithm.
Part 1: compression, decompression of json data
Need to speed up website by sending compressed json data then use lz-string library.
reference: http://pieroxy.net/blog/pages/lz-string/index.html
Compression and decompression code available in javascript (client side) and C# (server side) as well.
So you can compress and decompress data at either end while sending over TCP channel. Both code uses same algorithm.
Below code give you better illustration.
Step 1: Include script url to page
<
script
type
=
"text/javascript"
src
=
"lz-string.js"
></script
>
Step 2: Compress json data using lz-string.js at client side
// Use compressToUTF16 function to compress at client side
// Because decompression at server side will work properly with UTF16
var compressedData = LZString.compressToUTF16("Your Large JSON stringified data");
var myURL = "your URL";
var pageId = "Other Normal Data";
$.ajax({
type: "POST",
url: myURL,
data: {
lzStringData: compressedData,
pgId: pageId
},
async: true,
success: function (data, status) {
// wow!
},
error: function(data) {
// your error handling
}
});
While sending compressed data over TCP channel you can not able to see in plain format, because of UTF conversion so not able to read in fiddler.
Step 3: Uncompress you string and get the original at server side using C#
[HttpPost]
[ValidateInput(false)]
public string DecompressJson(string lzStringData, int pgId)
{
var jsonStr = LZString.decompressFromUTF16(lzStringData);
//process jsonStr using JsonArray, JsonValue, JsonObject
return "OK";
}
Note: Other libraries like LZW, LZMA, GZIP are not compressing much as compared to LZ-string, so I recommend to use this.
For further compression of data use GZip/Deflate compression.
Part 2: GZipping data-
How it works- If browser supports GZip/Deflate then it sends Accept-Encoding: gzip, deflate to server, and server responds back using Content-Encoding: gzip
1: Send gzip data to server using jquery ajax
$.ajax({
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("Content-Encoding", "gzip");
},
2: Send GZip content from IIS to client side use HttpModule or CustomAttribute
Use HttpModule to send the GZipped css and js files and make them cache able.
Use CustomAttribute to GZip response of controller method.
public class GZipContentAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext) {
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (AcceptEncoding.Contains("gzip")) {
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress);
Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "gzip");
}
else use DeflateStream
}
}
Decorate controller method with the attribute [GZipContent] so that response get encoded
Note: GZip or deflate both take CPU time to compress data, so use GZip/Deflate compression if required.
Download Code