RPC Docs.
If you know a little JavaScript then you know how to use Prim+RPC. In this guide, we'll outline the features of Prim+RPC and give examples of how to use them.
It is assumed that you have already set up the Prim+RPC client and server. If you have not yet then you may
follow the setup guide before continuing. The following are partial examples that demonstrate each
selected feature. We'll be working out of an imaginary file called example.ts
(a TypeScript file but feel free to use
regular JavaScript). For example's sake, we'll assume that this file is imported and used with Prim+RPC like so:
Of course, if you'd like to follow along then you can use any plugins that you'd like. The following examples will
modify the module given in example.ts
.
It's also worth noting that this guide is only meant to demonstrate usage of functions with Prim+RPC. You can find more features available with the Prim+RPC server and client by checking the configuration reference.
You can create a regular JavaScript function and use that directly with Prim+RPC. Remember to add the .rpc
property to
the function to tell Prim+RPC that it is allowed to be called. Also remember that the function result is wrapped in a
Promise and must be awaited (because we must await a response from the server).
By default, Prim+PRC can accept all types supported by JSON. These can be extended by using a custom JSON handler as seen in the next example.
Prim+RPC should be configured with a .methodHandler
on the server and a
.methodPlugin
on the client for method calls to work.
If you've set up a custom JSON handler then you can even use other non-primitive types like Dates directly with Prim+RPC!
You can also pass callbacks and files to functions!
If a function that you define on the server throws an error, that same error can be thrown on the client (as long as
.handleError
option is true
).
Functions used with Prim+RPC can accept callbacks. This is useful for listening to events on the server as they happen without having to poll the server at a set interval.
Prim+RPC should be configured with a .callbackHandler
on the server
and a .callbackPlugin
on the client for callbacks to work.
As you'll see in the next few examples, you can use multiple signatures on a function when using TypeScript. This is useful when accepting file uploads or uploading an HTML form where the type on the client is not the same as or is unavailable on the server.
You can upload files to Prim+RPC as long as your configured
.methodHandler
supports it. Files can be sent like so (this example
uses Node):
This function has two function signatures (when using TypeScript). The first signature is for the browser where the client will provide a File. On the server, a Prim+RPC plugin will read the file, save it to a temporary location, and then pass the location of the file back to the function wrapped in a Promise (since uploading the file from the client isn't immediate).
The current file-handling behavior of method handlers may be changed in the future to support Buffers or (possibly) Streams. Prim+RPC is in early stages.
If you have an HTML form on the page, you can pass that Form element directly to Prim+RPC. The Prim+RPC client will read the FormData given on the form and turn it into an object (preserving any files given on the Form) that will be sent to the server.
It's assumed that the HTML form used in this example has three text/email inputs named name
, email
, and message
.
Note that we defined a function signature in TypeScript intended for the browser (accepting a HTMLFormElement
) and the
function signature for the server is an object with the names of the given inputs (FormInputs
).
Functions don't have to be located at the top of your module in Prim+RPC. You can use nested modules like so:
You can set the context of the function based on the utilized method and callback handlers. Each handler used with
Prim+RPC will have its own context (for instance, a Request object in some server framework). This context can be
accessed from the function's this
object (Prim+RPC will bind that context to the function).
You may override this context or limit what is bound to functions by setting the .contextTransform
option, if
available, on the given handler.