webos-service API Reference

About webos-service

The webos-service module for Node.js provides an interface to the system bus, wrapped in familiar Node.js idioms. 

Example

This example registers a service (luna://com.example.helloworld/hello) on the webOS TV system bus, which responds to a request with a "Hello, World!" message.

//helloworld.js 
//simple service, based on webos-service API 
var Service = require('webos-service'); 

var service = new Service("com.example.helloworld"); 
service.register("hello", function(message) {				 
	message.respond({ 
		greeting: "Hello, World!" 
	}); 
});

Loading the webos-service library

This loads the webos-service module. The only thing exported from the webos-service module is the Service constructor.

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

webos-service API Reference

Service Object

Property/Method

Description

Service (busID)

Creates a Service object, which handles registering methods on the system bus.

  • busID: A string containing the bus address of the service, e.g., "com.example.helloworld"

For example,

var service = new Service(busID)

service.activityManager

The ActivityManager proxy object for this service. See the ActivityManager object for details.

service.busId

The busId used when creating a Service

service.call(uri,

arguments,

function callback(message){...}) 

Sends a one-shot request to another service.

  • uri: This parameter is the bus address of the service to send to, for example: luna://com.palm.wifi/status

  • arguments: This parameter is a JSON-compatible object which is encoded by the library and sent as part of the request.

  • callback: This callback function is called with a single parameter, which is a Message. See the Message object for details. 

service.register(methodName,

[function request(message){...}],

[function cancel(message){...}]) 

Registers a method for the service. When a request is made for that method, the callback function is called.

The callback gets one argument, which is a Message object

  • methodName: This parameter is the name of the method to register. You can group methods in categories by putting a category at the front of the methodName, separated by "/" characters, for example,

    service.register("/config/setup", function callback(message)\{...});

This method returns a Method object, which emits the request and the cancel events. If the request and cancel arguments are provided, they're bound to the request and cancel events, respectively.

service.subscribe(uri, arguments) 

Sends a subscription request to another service for services that support it.

  • The uri and arguments parameters are the same as the call method above.

This method returns a Subscription object, which emits events as responses come back from the other service. 

service.subscriptions 

All of the Messages currently subscribed to this service, indexed by their System Bus uniqueToken. 

Message Object

Message objects are used to represent messages coming in from other services or applications.

Property/Method

Description

message.cancel()

Sends a "cancel" message to the sender, which indicates no more responses will be coming.

This method is normally only used for subscribed calls. Single-shot calls do not require a "cancel" response.

message.category

The category of the method that was called.

message.isSubscription

This property is set to true if "subscribe": true is included in the payload, which indicates the sender wants to subscribe.

message.method

The name of the method that was called. This property is the part of the service URI that's before the method name.

message.respond(payload)

Sends a response to the requester.

  • payload: This parameter is an object that is JSON-encoded before being sent. Every response should include a returnValue property, which is set to true for success replies, and false for errors.

message.sender

The applicationID or busID of the message sender, depending on whether it was sent from an app or another service

message.uniqueToken

A string that uniquely identifies this request. It is guaranteed to be unique within any one run of the service. If you need to track service requests, this is probably the token you want to use.

Method Object

Property/Method

Description

service.register(methodName[,

requestCallback][,

cancelCallback])

Creates a Method object, which is an EventEmitter that emits the request() and cancel() methods.

For example,

var method = service.register(methodName[, requestCallback][, cancelCallback])

event "request"

This event is emitted when a message is sent to the registered method. The event handler receives the Message object corresponding to the request.

event "cancel"

This event is emitted when a sender of a message indicates that it is no longer interested in receiving replies. This event is only emitted for subscribed messages.

Subscription Object

Property/Method

Description

service.subscribe(uri, payload)

Creates a Subscription object, representing a request to uri.

  • uri: This parameter is the complete URI for the service method, e.g. luna://com.palm.wifi/status

  • payload: This parameter is an object, which is JSON-encoded before sending.

subscription.on("response",

function callback(message){...})

A response event is sent every time the other service sends a response. The callback receives a single Message parameter.

subscription.on("cancel",

function callback(message){...})

The cancel response indicates that the other service has canceled that subscription. It is a good idea to remove any references to the subscription at that time so that the message can be garbage-collected.

subscription.cancel()

Sends a cancel message to the other service, indicating that you no longer wish to receive responses. 

ActivityManager Object

Property/Method

Description

service.activityManager

This object represents a proxy to the ActivityManager System Bus service (com.palm.activitymanager), and also provides a timer that's used to control a service's lifetime.

Example,

var activityManager = service.activityManager;

activityManager.create

("name", callback)

Creates an activity with a reasonable set of default properties, for immediate execution by the service.

The service will not exist due to timeout while an activity is active. Note that the webos-service library creates a new activity for each request that comes in, so you don't need to create your own for simple cases. Your callback will be called with the new activity as an argument.

activityManager.create

(activitySpecification,

callback)

Creates an activity with the given activity specification.

This is useful when you want to create an activity with a callback, to cause your service to be executed at a later time. Your callback will be called with the new activity as an argument. 

You might call the complete method explicitly if you wanted to specify options to the complete operation, for example, to restart the activity, or change the triggers or schedule associated with the activity.

activityManager.complete

(activitySpecification,

options, callback)

The activity associated with a command will automatically be completed when you send a response.

You might call the complete method explicitly if you wanted to specify options to the complete operation, for example, to restart the activity, or change the triggers or schedule associated with the activity.

Navigation