Warning: Reason support is experimental. We are looking for beta-tester and contributors.

How to call a server-side function from client-side?

It is possible to call server-side functions in client-side. For security reasons, these functions must first be declared explicitely as RPCs (with the type of their argument).

let%rpc f (x : int) : string Lwt.t = ...
...
[%client ... f 4 ... ]

The syntax is provided by opam package ocsigen-ppx-rpc.

The server-side function (f in the example) needs to return a Lwt value.

Server functions are just syntactic sugar for pathless services returning OCaml values.

Note that you need to install ppx_deriving, and load our JSON ppx_deriving plugin in your project. The plugin is available as the Ocamlfind package js_of_ocaml.deriving.ppx.

If the function takes a more complex type, this type must have been declared with ppx_deriving. For example,

type t = int * string [@@deriving json]

Our infrastructure provides server-side data validation of the data sent, and prevents malformed data from crashing the server.