Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Adding Remote RPC Targets

There are scenarios where users may need to add remote RPC targets to facilitate communication between two endpoints that have no direct RPC connection channel. Consider the following 3 endpoints:

  • client
  • server
  • remote

There is a direct RPC connection between client and server, and server and remote. However, client and remote may need to send messages to each other as well. To do so, users can use an RPC proxy server.

Create a proxy server

The proxy server only supports HTTP to serve clients. It supports HTTP and Websockets to connect to the remote server:

HTTP:

var proxy = RpcProxy.new(["127.0.0.1:0"], getHttpClientConfig(srvUrl))

Websockets:

var proxy = RpcProxy.new(["127.0.0.1:0"], getWebSocketClientConfig("ws://" & $srv.localAddress()))

Registering remote target methods

The client can only make a call to the remote endpoint if the proxy (what's in the middle) has registered the remote RPC method:

proxy.registerProxyMethod("hello")

Registering methods

The proxy server can register its own RPC methods:

proxy.rpc(RpcConv):
  proc bye(input: string): string =
    "Proxy Bye " & input

Start the proxy server

After registering the RPC methods, the server can start serving clients:

await proxy.start()

Then usually runForever() or waitFor a program termination signal waitSignal(SIGINT). This will run the Chronos async event loop until the program is terminated.