The limits of request and response

In a request/response framework the server cannot start a conversation. Polling wastes requests and adds latency.

Compared with gRPC: Server-streaming methods cover subscriptions, but each stream is tied to the method that opened it and cannot be passed around. The server still cannot invoke anything on the client.

A second connection in the reverse direction requires the client to be reachable, which fails as soon as it sits behind NAT or a firewall.

Callbacks and transferred channel endpoints

In Remoc, channel endpoints are values that calls can return and accept. A method returns an rch::mpsc::Receiver and the client has a subscription; it returns an rch::watch::Receiver and the client always sees the latest value; the client puts a sender into a request and the server reports progress into it. Each channel keeps working after the call that created it has returned.

For callbacks, choose the shape that fits your interface:

  • A remote function wraps a closure with rfn::RFn. Pass it to the server, which calls it with typed arguments.
  • A client-served trait exposes several related methods. Implement a trait carrying #[rtc::remote] on the client and pass its generated client object to the server.

Compared with gRPC: Bidirectional streaming exchanges messages in both directions within a streaming method. Remoc supports that through channels, but a callback is a separate callable value with its own arguments and result. The server invokes it on demand over the existing connection, without the client polling for callback requests.

Return a channel from an RPC method

This trait is from the counter example. The watch method returns a watch channel receiver, giving the client notifications when the value changes. Its count_to_value() method similarly returns an mpsc receiver for a sequence. Use watch for the latest state; use mpsc when the receiver must read each item in a stream.

Service definition
#[rtc::remote(server(SharedMut))]
pub trait Counter {
    /// Obtain the current value of the counter.
    async fn value(&self) -> Result<u32, rtc::CallError>;

    /// Watch the current value of the counter for immediate
    /// notification when it changes.
    async fn watch(&mut self)
        -> Result<rch::watch::Receiver<u32>, rtc::CallError>;
}

Complete trait calling example → Pipelined calls save the round trips →

Server-to-client RPC callbacks: the pizzeria example

The pizzeria example uses the remote-function option. Its shared Progress type is a rfn::RFn<(String,), ()>: it accepts a progress message and returns no value.

  1. The client wraps its progress handler with Progress::new_1 and passes it to order().
  2. After each preparation step, the server calls progress.try_call(...) with a progress message.
  3. The supplied closure runs back in the client process while the order runs on the server.

Both directions use the one TCP connection opened by the client. The example includes all source files and run commands; OpenTelemetry export is optional.

Run the client callback example →

Questions

Does server push work when the client is behind NAT?

Yes. Everything runs over the one connection that the client opened, and channels and calls work in both directions on it. The server never has to connect to the client, so NAT and firewalls on the client side are no obstacle.

How is this different from gRPC server streaming?

A gRPC stream is tied to the method that declared it. A Remoc channel is a value: a call can return it, a message can carry it, and it can be stored and used long after the call completed. A gRPC bidirectional stream carries messages in both directions within a client-initiated call; it does not by itself let the server invoke a client-defined RPC method. With Remoc the client can also pass a remote function or serve a trait of its own over the existing connection.

Can an RPC method return a channel or stream of updates?

Yes. Return an rch::mpsc::Receiver for a sequence or an rch::watch::Receiver for the latest state. The caller receives the endpoint as a normal result and continues receiving updates after the RPC method returns. Both patterns are implemented in the counter example.

What happens when the client disconnects?

Sending into a channel whose remote end is gone fails with an error, and calls on a disconnected client fail likewise. The server notices at the point of use and can drop its state for that client.

Can the server push without the client asking first?

The client has to hand the server a way in once, by calling a method that returns a channel, by including a sender in a message or by providing a client for its own trait. From then on the server pushes whenever it wants.

Can I use this between Rust and another language?

No. Remoc's protocol is defined by Rust types serialized with Serde, so both peers must be Rust programs. For cross-language services a schema-based system such as gRPC is the better fit.

Related pages

Remoc vs. other RPC libraries compares this model with tarpc, gRPC and Cap'n Proto. Channels between processes and machines explains the channels used for server push. For the local case of a daemon notifying clients, see IPC between Rust processes.