Remote Procedure Call
dnet’s rpc module (enabled with the “rpc” feature, which is enabled by default) provides a type-safe implementation of Remote Procedure Calls (RPC) that works over any transport supported by dnet.
What is RPC
Remote Procedure Call (RPC) allows code running in one execution context to invoke functions that execute in another context as if they were ordinary local function calls.
The two sides may be running:
- in different processes,
- on different computers connected over a network,
- in different browser tabs,
- inside Web Workers,
- or on any other pair of peers connected through a
dnettransport.
With dnet, you define your API once as a Rust trait, implement it on one side, and obtain a strongly typed client on the other. Serialization, message routing, request/response matching, and asynchronous communication are handled automatically.
Steps
Implementing an RPC service consists of a few simple steps:
- Define your service interface (see API section).
- Implement a producer for your service (see Producing section).
- Establish a connection between two peers using any supported
dnettransport. - On the server side, register the service by calling
produce(...). - On the client side, create a typed client by calling
consume(...).
Once the connection is established, you simply call methods on the generated consumer (see Consuming section). The RPC layer transparently serializes arguments, sends requests over the selected transport, executes the corresponding server-side method, and returns the result.
Example
For a complete example, see the RPC example in the dnet repository.