JavaScript Services

Currently, HP provides webOS apps access to a rich set of application and system services available on the public bus. With webOS 2.0, third party developers can now "roll their own" services. Besides powering Synergy connectors, JavaScript services strengthen webOS's support for background processing and add new capabilities such as low-level networking, filesystem access and binary data processing to the webOS technology stack.

In this section:

Why Services?

Services can provide your app or apps with a number of distinct advantages:

  • Faster performance -- Services run in their own single-threaded instance of V8.

  • Offloaded processing -- Services can run in the background and offload processing such as file downloads and network monitoring.

  • Shared processing -- Services can perform tasks for one or more apps.

Service examples:

  • Synergy service -- Synergy connectors that integrate with resident PIM applications such as contacts, calendar and messaging will typically need to create a Synergy service implementing a connection to a cloud data source for syncing and validation. Please note that a "Synergy service", in prior documentation, may have been referred to as a "Transport" or "Transport service".

  • Location tracking -- On a timed interval, a service could log GPS coordinates to a file or db8 storage.

  • Chat client -- A service could monitor the network or cloud for instant messaging requests.

  • Media download -- An app could request a service to download media files. After the app ends, the service could remain running in the background, monitoring the network and downloading files when it becomes possible to do so.

JavaScript for Services

The popular Node.js runtime environment is built into webOS 2.0., allowing you to develop both webOS apps and services in JavaScript. Developers can write services using a rich set of JavaScript libraries. Besides the open-source Node.js, these include webOS's Node.js add-ons and Foundation libraries of utility JavaScript APIs.

Node.js

Figure 2: The Node.js Stack

Figure 2: The Node.js Stack

Node.js is an event-driven I/O framework for the V8 JavaScript engine intended for implementing scalable network programs such as web servers and streaming servers. Since Node apps do not require a UI or browser ("DOM" less) -- they run in their own instance of V8 -- they are more related to server-side JavaScript.

Node.js uses an event loop instead of threads, allowing it to scale to potentially millions of concurrent connections. It exploits the fact that servers spend most of their time waiting for I/O operations, which are much slower than in-memory operations. Every I/O operation in Node.js is asynchronous, which allows the server to continue processing incoming requests while I/O takes place in the background. This event-based model makes Node.js very fast, and makes scaling real-time applications very easy. JavaScript is extremely well suited to event-based programming because of its anonymous functions and closures, which make defining inline callbacks very simple.

In addition, the active Node development community provides support and a rapidly growing library of modules that you can use in your webOS services.

Foundations

Services (as well as apps) can take advantage of HP's Foundation libraries of JavaScript utilities. Foundations provides utilites for:

  • Object, string, and array processing
  • AJAX calls
  • Accessing webOS services
  • Asynchronous processing (Futures)
  • Map reduce functionality
  • Implementing Finite State Machines
  • XML and JSON conversion and validation
  • Creating class and error objects

See the Foundations API reference for more information.

Next Steps

To begin understanding how to implement services, you should step through the "Creating a Hello World Service" procedure. Like all Hello World-type implementations, the intention is to strip the process down to a bare minimum, clearly showing what is involved and establishing a base for further development. Through this procedure, you will learn how to create and package a service, install it on the public bus, and call it from a JavaScript app.

After that, if you are interested in creating a Synergy connector with a JavaScript Synergy service, you can step through the "Creating a Synergy Contacts Package" tutorial. This will step you through the basics of creating and installing a Synergy app/service/account package, integrating with the webOS Contacts app, implementing a bare-bones Synergy service, and authenticating and syncing with an outside data source, in this case, Plaxo, an online address book and social networking site (http://www.plaxo.com).