HTML5/JS Tutorial HTML5/JS Tutorial Part IV

View the Project on GitHub FatFractal/hoodyoodoo

Server Extensions

The next activity we need to do is collect some key metrics that we want to show in the game. We could define a StatsObject resource to be persisted on the API, write four Event Handlers that would update these statistics every time any of their respective events occur to keep it up to date. Instead, we will create an ephemeral object with a Server Extension that assembles the statistics on the fly and returns the data to the client.

If you wish, you can download the complete Part IV project file from Download Tutorial Part 4 Sample Files and then unzip and then either run newapp again from this directory to use this application or copy the files that are enclosed to the application directory you created in the previous section Installing FatFractal allowing the copy to replace all the previous files.

If you have not scaffolded your app already, it is not a problem, just run ffef newapp, using your subdomain, as before from the directory with the Part IV files you have just downloaded and it will set up all the things that you need.

> $HOME/ff/ffnsbin/ffef newapp hoodyoodoo <your subdomain>

Note: The subdomain refers to the organization name you signed up with, e.g. acme in acme.fatfractal.com

Note: When you download the project, the client application is configured to use a API that is already deployed and populated with data. This means you can run the application on your simulator immediately. To use your API, you should edit the Hoodyoodoo.java file as before to point to the API that you have set up.

Adding server extensions (Custom endpoints) to your API

Our open platform lets you extend the API as easily as we allow you to create instant models on the API. For example, to make your app interesting, you might want to create server extension such as an algorithm you want to run in the Cloud,etc. In fact, we have designed our engine so that you can extend the FatFractal platform itself--more on that later.

In this tutorial we will show you a simple-yet-powerful method to extend the server with an algorithm you wouldn’t want running on the client-side.

Another very useful capability is the ability to create Server Extensions. These are additional actions that can be called from your client that perform whatever functions that you wish on the server. This can be a tremendous benefit to your client in terms of performance as it can reduce the network traffic to your application as well as reduce the complexity and resource demands for your client application.

For example, here is a simple server extension that collects some interesting statistics.

Server Description for the Server Extension

The Server Extension must be defined in your application.fddl file that is in the ff-config directory that was created for you during scaffolding. See FatFractal Definition Language for more information.

The FDDL looks like:

CREATE EXTENSION /Stats AS javascript:var f = require ('scripts/AggregateStats'); f.aggregateStats(FF_EXTENSION_REQUEST_DATA_JSON);

This entry tells the NoServer Framework to create a server-side extension with the URL

http://<base URL>/ff/ext/AggregateStats

that can be accessed via all HTTP methods, in our case, we will use a Get and are expecting a single object to be returned.

A JavaScript API Extension for you API

We will now create a Server Extension JavaScript file that defines an additional object model called StatsObject as well as several Queries that will aggregate the desired information and return the data to the client.

To create this Server Extension, we will create a javascript file called "AggregateStats.js" in the /ff-scripts directory. The basic structure of the file is:

var ff = require('ffef/FatFractal');

function aggregateStats() {
    // some code...
}
exports.aggregateStats = aggregateStats;

FatFractal.js is the server-side encapsulation of the NoServer API that is available to the EventHandler functions. For more information, see the HTML5/JS SDK Reference.

We next create a new object called StatsObject that we will populate using this server extension. Note that this object is not persisted, it is populated and returned on the fly by this extension. Of yourse, you could easily persist the object if you wish, but in this case is not required.

function StatsObject() {
    this.totalUsers = null;
    this.totalRatings = null;
    this.totalCelebrities = null;
    this.yourRatings = null;
    return this;
}

Next we get the data from the HTTP request:

var data = JSON.parse(json);

Instantiate a new StatsObject and populate it using three queries and, in this case calculating the lengths of the returned Arrays. Note: in this case, we are getting only the guids of the respective resources which is more efficient than retrieving the entire object data. This can be quite a useful method for efficient queries where all you need to get is the set of guids.

statsObject = new StatsObject();
statsObject.totalUsers = ff.getAllGuids("/FFUser").length;
statsObject.totalRatings = ff.getAllGuids("/WouldYa").length;
statsObject.totalCelebrities = ff.getAllGuids("/Celebrity").length;

var createdBy = data.httpCookies['userGuid'];
statsObject.yourRatings = ff.getArrayFromUrl("/WouldYa/(createdBy eq '" + createdBy + "')").length;

Now, we construct the response a return it. Note, it is not required that you set all the response codes, as default values will be returned, but you are free to set them if you wish.

r = ff.response();
r.responseCode = "200";
r.statusMessage = "Getting the stuff you want in one round trip";
r.mimeType = "application/json";
r.result = JSON.stringify(såtatsObject);
return r;

To make your API aware of these new items, you need to deploy again (have you noticed how little you have to fiddle around with your API so far??) by doing the following:

If you are running in the Cloud

If you haven't already, go to the Console and add an application called hoodyoodoo to <your subdomain>.

From the hoodyoodoo application directory, deploy the modified hoodyoodoo API to the NoServer Public Cloud, you use the command line deployFFFabric:

> $HOME/ff/ffnsbin/ffef deployFFFabric

In a few seconds, your updated application will be available at http://<your subdomain>.fatfractal.com/hoodyoodoo

Running locally

Assuming that the FatFractal Engine is running, you can also deploy to your development machine using the command line deploylocal:

> $HOME/ff/ffnsbin/ffef deploylocal

In a few seconds, your modified application will be available at http://localhost:8080/hoodyoodoo

NEXT: Update the Client App Code