JavaScript Service Basics

What is a webOS JavaScript service?

JavaScript services (JS services) on webOS provide the way for apps to do work, even when the app isn't running. They also provide access to platform features that aren't usually available to web apps.

JS service on webOS TV has the following characteristics:

  • It is written in JavaScript and created using Node.js.
  • It runs in the background on webOS TV.
  • It provides additional access to platform features such as low-level networking, file system access, and binary data processing.
  • It performs tasks for one or more apps.

Use cases for webOS JavaScript services

Typically you will use a service either to perform tasks that a webOS app can't do or to do work by service in the background. Some examples of how services have been used in webOS apps in the past include:

  • Downloading attachments in the background for an email reader
  • Uploading images to a picture-sharing website from an app
  • Performing a long-running computation or file operation

About Node.js

Node.js was created as a framework for server-side JavaScript applications. Services in webOS are not quite like web application servers (in particular, because they don't stay running all of the time), but the basic framework of an asynchronous I/O and network stack makes much sense as an extension of the webOS app environment.

Supported Node.js version

webOS TV supports the different versions of Node.js core modules depending on the webOS TV platform version as below.

webOS TV platform versionNode.js version
webOS TV 24v16.19.1
webOS TV 23v12.22.2
webOS TV 22v12.21.0
webOS TV 6.0v8.12.0
webOS TV 5.0v8.12.0
webOS TV 4.xv0.12.2
webOS TV 3.xv0.12.2
webOS TV 2.xv0.10.25
webOS TV 1.xv0.10.15
Caution

When developing JS services, you should keep in mind supported Node.js version by webOS TV version. For example, in webOS TV 4.x or lower, the ES6 syntax, such as the following, is not supported.

  • const, let
  • arrow function
  • destructing

You can check the syntax supported by Node.js version at node.green.

Using Node.js core modules

To use the Node.js core module, add the module with a top-level identifier to your service as the following code.

var Service = require('webos-service');
var md5 = require('md5');

var service = new Service('com.yourdomain.helloworldservice.service');

service.register('hello', function (message) {
  message.respond({
    data: 'Hello, ' + md5(message.payload.name) + '!',
  });
});

Using third-party modules

There are many third-party modules for Node.js. To use the third-party modules, follow the next steps.

  1. Use NPM (Node Package Manager) tool on your computer to install the third-party module. The module is installed in the node_modules directory under the node.js installation folder. See Node Packaged Modules (NPM) for the search and installation of these modules.
  2. Create a node_modules directory in your service's root directory.
  3. Copy modules from the node_modules directory under the node.js folder to the node_modules directory under your service's root directory.

Now, add the third-party module to your service as the following code. The node will find the module from the node_modules directory first.

var md = require('md5');
Important
  • Downloading files in a web app to a TV device is not supported. A service may download files, but yet those downloaded files are not accessible from web apps.

  • You cannot use modules that include C/C++ addons. You must use modules implemented with JavaScript only.

No Headings