Creating JS Service

Creating JS Service 

To create a JS service, you can use ares-generate command which generates a JS service project from template. If you do not have an app project where you call the service, you should create an app project before creating JS Service. Note that JS Service cannot be packaged alone.

The service name must begin with the app name. If you do not follow this naming rule, the service packaging dose not work normally. For example,
  • App name: com.example.myapplication
  • Service name: com.example.myapplication.myservice

 

The usage of ares-generate command and options are as below. Depending on the app type, specify the -t option with the appropriate template. Here are some examples of the different uses:

 

Create Enyo App Template with CLI

ares-generate -t moonstone-2014 -p "id=com.yourdomain.myapp" samplePrj

 

Create Any Web App Template with CLI

ares-generate -t basic -p "id=com.yourdomain.myapp" samplePrj

 

After you have created app template, create JS service template with service name beginning with the app name.

 

Create JS Service Template with CLI

ares-generate -t js_service samplePrj -s com.yourdomain.myapp.service

 

For usage of ares-generate command and options, see ares-generate

You can also use Plugin for Sublime Text to create JS service. See Using Plugin for Sublime Text.

Structure of JS Service

Once you generated the service template, JS service directory should be configured as below. 

JSservice_creation.png

 

  • APP_DIR: Directory of the application
  • SERVICE_DIR: Directory of the service where a subfolder named with the service name and JS Service files are located. Multiple service folders can be located in SERVICE_DIR.

 

For description on service sub-directory file, see the next section. 

Registering JS Service

A service is made up of the following files: 

  • package.json: Configures the service meta-data and point main service file. This file is needed for packaging (Related with Node.js)
  • services.json: Defines the services and its methods. This file is needed for registration JS service on Luna bus
  • JavaScript Service Source files: Service implemented files provide various use case of JS service.

The package.json File

As a service program is a Node.js module, a service must have a package.json file. The minimal package.json looks something like this:

{
	"name": "com.mycom.helloworld.service",
	"main": "helloworld_webos_service.js"
}
  • In name property, write your service name.
  • In main property, write your main service JavaScript file name.

 

There are quite a few other values one can set in the package.json file. For complete specification of package.json, see NPM (node package manager). 

The services.json File

This service configuration file describes what commands the service provides on the webOS bus. Define commands to be called from other apps and services. For more information, see services.json.

{
	"id": "com.mycom.helloworld.service",
	"description": "Sample helloworld service",
	"services": [{
		"name": "com.mycom.helloworld.service",
		"description": "Sample helloworld service"
	}]
}

Main Service JavaScript File

In main service JavaScript file, you should define a single command assistant to execute the command defined in the services.json file.

 

Before registering a service into your code, you should load the webos-service module. The webos-service module for Node.js provides an interface to the system bus, wrapped in familiar Node.js idioms.

 

This loads the webos-service module. 

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

 

The following JavaScript example registers a service (luna://com.mycom.helloworld.service/hello) which responds to a request with a "Hello, World!" message.

var service = new Service("com.mycom.helloworld.service");

service.register("Hello", function(message) {        
	message.respond({
                Response: "Hello, World " + message.payload.name + "!"
       });
});

 

For more detail about webos-service module, see webos-service API Reference.

Packaging JS Service

Once registering service and implementing app and service are done, you need to package them into an .IPK file.

 

For packaging JS service, usage of ares-package command and options are as below. As mentioned in the figure above, APP_DIR and SERVICE mean app and service directories. APP_DIR and SERVICE_DIR can be both absolute and relative paths. 

ares-package APP_DIR SERVICE_DIR

 

Here are some examples of the different uses:

 

Packaging service with absolute path (in Windows)

ares-package c:\samples\app1 c:\samples\service1

 

Packaging service with absolute path (in Linux/Mac OS)

ares-package ~/samples/app1 ~/samples/service1

 

Packaging service with relative path (in Windows)

ares-package app1 service1

 

Packaging service with relative path (in Linux/Mac OS)

ares-package ./app1 ./service1

 

Packaging multiple services an app (in Windows)

ares-package app1 service1 service2

 

Packaging multiple services an app (in Linux/Mac OS)

ares-package ./app1 ./service1 ./service2

 

For usage of ares-package command and options, see ares-package.

 

Navigation