Using Async_rpc_kernel.Rpc with js_of_ocaml

I’m trying to build a web application using js_of_ocaml (incr_dom to be more precise), and I need to implement rpcs inside the web app. Specifically, I would want to send an Async_rpc_kernel.Rpc to a server given its host and port. However, since the unix part of Async is not supported with js_of_ocaml, I won’t have access to libraries such as Async_unix.Tcp, which prevents me from using the usual way of establishing a Rpc connection. (e.g. I would normally use Tcp.Where_to_connect.of_host_and_port combined with Rpc.Connection.client to establish a connection)

How may I go around it?

JavaScript doesn’t give you direct access to create TCP sockets, so you won’t be able to use anything like Async_unix. However, the Async_js library provides some of same RPC functionality using a WebSockets for the transport. See https://github.com/janestreet/async_js/blob/master/src/rpc.mli for the interface.

The server will also have to be configured to accept RPCs over WebSockets instead of (or in addition to) plain TCP.

2 Likes

The server will also have to be configured to accept RPCs over WebSockets instead of (or in addition to) plain TCP.

Can you say more about how one would configure this? Wouldn’t there need to be a Websocket implementation of the RPC server itself?

Thanks for the reply! I looked over the Async_js rpc mli file, and it appears that most of the modules inside are straight from Async_rpc_kernel. The only difference is the Connection module, which it has a different implementation. Won’t that mean that the server can just support RPCs over plain TCP, and only the client needs to use the Connection module?

Has anyone tried using the Async_websocket module yet? I tried to install it from opam and it succeeded. But I’ve having difficulties compiling it in dune and utop.

The dune file for version v0.13.0 (the version present on opam) seem a little strange. They don’t define the public_library in the dune stanza. Looks like that was added with this commit https://github.com/janestreet/async_websocket/commit/a37f4c1f084e5c2273c50792ac8069e018d858da recently. If you pin the version present on github you should be able to use it.