JSS RPC Reference

CompleteFTP makes it very easy to call JSS functions as Remote Procedure Calls (RPC). It does this by automatically generating client-side proxies for calling server-side functions, meaning that adding a remotely invocable server-side function is as easy as adding a client-side function.

Which functions are available via RPC?

Functions declared in the global scope and those referenced by the publish function (without the serverOnly flag) are automatically made available via RPC when the URL of the .jss file in which they're defined are referenced by allowed script tag in HTML. The Javascript code required to call them is automatically generated by CompleteFTP (see here).

The following example illustrates some usages:

function globalFunction() {
}

(function () {
    function publishedFunction() {
    }

    function serverOnlyFunction() {
    }

    function internalFunction() {
    }

    publish("publishedFunction", publishedFunction);
    publish("serverOnlyFunction", serverOnlyFunction, { serverOnly: true });
}());

Both globalFunction() and publishedFunction() are available for RPC, the former because it's a global function and the latter because it's published using the publish function. Neither internalFunction() and serverOnlyFunction() are not available via RPC, but serverOnlyFunction() may be called by any other .jss file that includes it using the include function because it's published with the serverSide flag.

Calling via RPC

Two forms of RPC are supported: JSON-RPC and form-based RPC.

JSON-RPC

CompleteFTP uses JSON-RPC 2.0 for client-server communications. JSON-RPC is a minimalist JSON-based RPC protocol. For example, the call to the hello() function in the Introduction to JSS is marshalled as

{"jsonrpc": "2.0", "method": "hello", "params": ["world"], "id": 1}

which returns the result in the following form:

{"jsonrpc": "2.0", "result": "Hello world from the server", "id": 1}

Errors are returned as:

{"jsonrpc": "2.0", "error": {"code": 0, "message": "An error occurred"}, "id": 1}

Form-based RPC

CompleteFTP also supports form-based RPC via standard HTTP GET and POST. If an element named method is included in the form then the Javascript function with that name will be called. The arguments in the call will match the elements in the form - in the same order. Thus the hello() function in the Introduction to JSS may be called by HTTP-POST using the following HTML:

<html>
<body>
	<form method=post>
		<input type=hidden name=method value=hello>
		<input type=text name=firstName>
		<input type=submit>
	</form>
</body>
</html>

Alternatively it may be invoked by HTTP-GET via the following URL:

http://hostname/directory/script.jss?method=hello&firstName=world

In both these cases the result is returned in the JSON-RPC format, i.e.

{"jsonrpc": "2.0", "result": "Hello world from the server", "id": 1}