Friday, March 15, 2013

Using compression in REST communication between applications

When you are working on a large project, there are alot of situations that the project is composed of more than one application. The problem here is how to solder everything together. There are alot of ways you can handle communication between applications, for example you can use XML, JSON, HTTP Requests, etc. If you are using PHP for building your project maybe the easiest way of handling communication is by using JSON.
There are two buildin function that you need to look at before starting:
We will use these two functions to serializing and deserializing the data that we need to send between applications.
So what we need now is methods for encoding data and sending the encoded data to a certain url:

Now we have two static methods:
- send - sends the json string via http post request
- encodeParams - convert an object or array to json string
Because we don't need to wait for the request to finish we add:
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
This two lines of code mean that the connection will be broken after one second.
If there is alot of data that needs to be tranfered then we will need to use somekind of data compression. To do that in php we can use the zlip library.
We update the encodeParams method to handle zlip compression.
1. We convert the object to a json string
2. We compress that string using gzcompression
3. Because the compressed string has many bytes that connot be send via a post request, we need to encoded. We use base64 firs because we wont gain much if url encode it first.
4. We encode the string with urlencode, because we need to send it via a http post request.
To recieve compressed data from another application we need to decode the post prameter:

Here is the complete class:

No comments:

Post a Comment