Building background services with webOS 2.1

1 BY khague

As you know, the webOS 3.0 SDK is now available to developers in Early Access. But here are a few things developers with existing webOS apps can keep in mind to optimize their 2.x-based apps, particularly around building background services.

With the release of webOS 2.1 SDK, HP has added service capabilities to 3rd party applications. These services can do things like cache data, save and read files, make web service calls and even run a web server! Below are a few points to remember when integrating services into webOS applications. A suggested starter is the BasicService found in the SDK 2.1 package.

Endpoints and other high-level service parameters are set within the services.json file. The commandTimeout property tells the service how long to execute any give endpoint before throwing a timeout to the caller. The default is 60 seconds. Developers should not modify this setting as it protects users from power draining race conditions.

The activityTimeout property tells the service manager how long to keep the service open after an endpoint has returned data to the caller. HP has optimized this value. It is recommended developers do not try to override this value. If the service does not need to run constantly, try using activity manager to have the service wake and handle events.

The serviceAssistant is an optional assistant and is executed once when a service starts. Put things like data initialization and other special routines used across all endpoint functions. Remember, this is called only once when the service starts…not for each endpoint call. To create, add the assistant property to the services.json file:

....
"services": [{
"name": "com.palmdts.servicesample.service",
"description": "helloworld example",
"assistant": "ServiceAssistant",
"commands": [{
....

and create the target function within the assistants.js file.


ServiceAssistant = function() {};
ServiceAssistant.prototype.setup = function() {
console.log("---->Service Assistant Executed");
};

Prologue.js is used with a few HP samples included within the 2.1 SDK. This is a standalone JavaScript file typically included at the top of the sources.json list. Use this to create and store global objects for use across all endpoints. This is different from serviceAssistant in that any object created here is globally scoped and directly accessible by endpoint functions.

Node.js is also now available for developers within the 2.1 SDK. Very little has to be done for the developer to access Node.js functionality. In fact all 3rd party services are being executed by Node!

To Enable access to Node.js objects, simply add a prologue.js to your service like this example:

[{
"library": {
"name": "foundations",
"version": "1.0"
}
},{
"source": "prologue.js"
},{
"source": "assistants.js"
}]

and within the prologue.js file like this:

//Node.js - get require object
if (typeof require === "undefined") {
require = IMPORTS.require;
}
var sys = require('sys');
var fs = require('fs');

//end Node.js stuff

 

//Foundation libraries - setup Futures
var Foundations = IMPORTS.foundations;
var Future = Foundations.Control.Future;

Now within a file the node object sys and fs are available from any endpoint.

For more information on Node.js see the .2.3 docs here: http://nodejs.org/docs/v0.2.3/api/html/

Stay tuned. In the coming weeks we’ll post more tips and tricks about services and node.js.

Comment (1)