Skip to content

Instantly share code, notes, and snippets.

@sspencer
Created May 12, 2010 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sspencer/399025 to your computer and use it in GitHub Desktop.
Save sspencer/399025 to your computer and use it in GitHub Desktop.
NodeJS BrowserPlus Service Sketch
// A rough sketch of how a NodeJS service might look
// Take 2 - keep services flat.
service = require('browserplus').service;
log = require('browserplus').log;
service.name = "HelloWorld";
service.version = "1.0.1";
service.doc = "A hello world service for BrowserPlus";
// A hook that is called just after the service is loaded
service.atStartup = function() {
log('info', "we're starting up!");
};
// A hook that is called just before the service is unloaded
service.atShutdown = function() {
log('info', "we're shutting down!");
};
// initialize is a reserved word and is the constructor, invoked
// when a page allocates an instance of this service
service.initialize = function(args) {
log('info', "new session started for " + args.uri);
};
function myPrivateFunc() {
// functions not exported are not visible
// outside of this module
}
// exported below so it can be called through browserplus
function hello(trans, arg) {
// args is how you can access arguments. callback arguments
// are proper objects that support an "invoke" method
args.cb.invoke("Hi there " + args.who);
trans.complete("hello " + args.who);
};
// first param is function to be exported
// second param is function doc
// third param+ is argument doc
service.export(
hello,
"A function that says hello",
"<name: string> the person's name",
"[greeting: string] the optional greeting (defaults to Hello)",
"[cb: callback] the function to invoke with the greeting"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment