Home Up Back Forward Leave Message


Overview

The code is available for download from:

PHP

Please note that for now I only present a PHP client. For a PHP server we need to make use of additional functionality like for example memcached or a database to keep messages and channels between separate sessions.

JSON handling in PHP

JSON marshalling and unmarshalling (or encoding and decoding as it is called in PHP) is build in standard and very straight forward. To generate JSON objects we only need to create an associative array. PHP wil encode it as a JSON object. Thus for example to create the logon object to pass on to the microservice it will look as follows:

   $obj = json_encode(array('username' => 'daffy', 'password' => 'duck'));
Above will generate the following JSON object:
    {
      "username": "daffy",
      "password": "duck"
    }
Unmarshalling or decoding is just as easy:
    $jsonobj = '{"username": "daffy", "password": "duck" }';
    $obj = json_decode($jsonobj);
    echo "username=" . $obj->username . " password=" . $obj->password;

HTTP handling of calls in PHP

In order to do a post (or get) to a url file_get_contents is used, but because we are going to submit JSON in the body we need to create a context stream and provide that to the file_get_contents function.

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: text/json',
            'content' => json_encode($callbody)
        )
    );

    $context  = stream_context_create($opts);

    $retbod = file_get_contents('http://localhost:8080/' . $call, false, $context);     
Create and the associative array (embedded within each other) with the method being POST, and the call body encoded to JSON. Create a stream context based on this and then call file_get_contents with the url of the microservice function and the stream context created with the JSON data to post. The result is placed in a variable which can obviously now be passed to json_decode

Javascript

Please note that the Javascript client is only for browsers and then JQuery is used to do the actually calls. For other frameworks you can do similar implementations.

JSON handling in Javascript

To generate JSON from a Javascript object one only has to do the following:

    var logon = {"username": "daffy", "password": "duck"};
    var jsonobj = JSON.stringify(logon);
The JSON.stringify is build into all the newer Javascript implementations of the newer browsers. We are going to make use of JQuery to do the call for us and JQuery is so nice to actually convert the returned JSON to a Javascript object for us. See next section.

HTTP handling of calls in Javascript

As mentioned I use JQuery to do the actual call to the service and it can be done as follows:

    $.post("http://localhost:8080/logon",JSON.stringify({"username": "daffy", "password": "duck"}), function(data) {
        if (data.status) {
            console.log("Session key=" + data.data);
        } else {
            console.log("Error: " + data.data);
        }
    });
The $.post takes an url to call, the data to send and a function to call with the data that was returned. In the example the Javascript object is converted to JSON and send in the body to the server. When the server sends something back the third parameter is called (a function declaration in this instance) with the data returned already converted from JSON to a Javascript object.