Defining an IPC interface
The operating system gives you a byte stream for inter-process communication, but what the processes actually need are commands with typed arguments and results, progress and event notifications, and an interface that can grow without breaking installed clients.
Other IPC approaches: A hand-rolled protocol over a UNIX
socket means framing, a message enum and dispatch code that grow with every
command. JSON over stdin and stdout is quick to start with, but untyped and
without a versioning story. D-Bus integrates well with system services, but
brings a bus daemon and its own type system. gRPC over a UNIX socket uses a
separate .proto schema and generated message and service code.
Your Rust traits as the interface
With Remoc, the daemon's interface is a Rust trait carrying
#[rtc::remote],
which generates the client and server implementations. Arguments and results
are ordinary Serde types. A method can return a channel for later events,
or accept a channel endpoint inside a request. The
channels work in both directions over
the same connection.
Since Remoc serializes with the Postbag codec, the interface can evolve. You can add fields, methods and enum variants and an updated daemon keeps understanding an older installed client, without version negotiation code.
The transport is whatever local byte stream suits you: a UNIX socket, the stdin and stdout pipes of a child process, a named pipe on Windows or a TCP connection on localhost. Remoc implements no transport itself, so nothing else is pulled in.
A local daemon and its clients
The timer daemon example is a complete daemon and CLI, using Unix sockets on Unix and named pipes on Windows. Timers belong to the daemon: a client can add a reminder and exit while the timer keeps running.
timerctl add 30s "Check tea"schedules a reminder and exits.timerctl listlists pending reminders.timerctl watchreceives future completions on a broadcast receiver returned by an RPC call.timerctl add 30s "Check tea" --waitsends a oneshot sender inside the request and waits for that timer's completion.
The add call returns an identifier, not the eventual completion. To wait,
the client creates a oneshot channel, keeps its receiver and includes
the sender in the serializable NewTimer struct:
use remoc::prelude::*;
use std::time::Duration;
use timer_daemon_example::{local, NewTimer, TimerService, TimerServiceClient};
let stream = local::connect(local::DEFAULT_ENDPOINT).await?;
let (reader, writer) = tokio::io::split(stream);
let client: TimerServiceClient =
remoc::Connect::io(remoc::Cfg::default(), reader, writer).consume().await?;
let (notify, completion) = rch::oneshot::channel();
let id = client.add(NewTimer {
after: Duration::from_secs(30),
message: "Check tea".into(),
notify: Some(notify),
}).await?;
println!("Added timer {id}");
let finished = completion.await?;
println!("Finished {}: {}", finished.id, finished.message);
consume() establishes Remoc and receives the RPC client handle
sent by the daemon's provide(). Both helpers start the
connection I/O in a background task. The daemon separately runs
server.serve() to dispatch calls to its service implementation.
The oneshot reply and RPC traffic share the same connection. Watchers use their own connections and receive the same completion through broadcast channels. Disconnecting a waiting client does not cancel the timer or stop other watchers.
Choosing the local transport
The example's local::connect and listener hide only the
operating-system-specific setup:
- Unix:
UnixListenerandUnixStreamuse a filesystem socket path, such as the example's defaulttimerd.sock. - Windows: Tokio's named-pipe server and client use a
name such as
\\.\pipe\remoc-timerd. The listener creates another pipe instance for each new client.
Each accepted stream is split and passed to Connect::io.
The shared trait, timer implementation and client calls are unchanged
between platforms. To adapt a TCP-based service, replace its listener
and connect code in the same way.
Child-process IPC over stdin and stdout
For a worker or plugin that the parent starts itself, RPC can use pipes instead of a listening socket:
- Spawn the child with
tokio::process::Commandand piped stdin and stdout. - In the parent, pass the child's stdout as the reader and its stdin
as the writer to
Connect::io. - In the child, pass its own stdin as the reader and stdout as the
writer to
Connect::io.
The existing child-process transport example shows both sides. It uses placeholder request and response types to illustrate the transport setup; substitute your application's types. Further channels, callbacks and remote trait clients travel over that same connection.
Keep stdout reserved for Remoc's protocol and send logs to stderr. Retain the child handle if the parent needs to wait for its exit or restart it.
Questions
Does this work on Windows?
Yes. Remoc runs over anything implementing AsyncRead and AsyncWrite, which includes Tokio's named pipes on Windows. The timer daemon example uses named pipes on Windows and Unix domain sockets on Unix, with the same RPC interface.
Can I talk to a child process I spawned?
Yes. The standard input and output pipes of a child process form a byte stream that Remoc can run over. Connect the parent's reader to the child's stdout and its writer to the child's stdin. The child uses its own stdin as the reader and stdout as the writer. Keep stdout exclusively for the protocol; use stderr for logs. The transport documentation contains the parent and child code.
Can several clients connect to one daemon?
Yes. The daemon establishes one Remoc connection per client, and their RPC servers can share the same service object. A broadcast sender can notify receivers on all of those connections, as in the timer daemon example.
What happens when daemon and client are different versions?
Remoc serializes with the Postbag codec, which supports schema evolution. Fields and enum variants can be added, removed and reordered, so an updated daemon keeps working with an already installed older client and vice versa.
How does this compare to D-Bus?
D-Bus is the right choice for talking to system services and desktop components, since it is a shared bus with cross-language support. Remoc fits when both processes are your own Rust code and you want your Rust types as the interface, without a bus daemon in between.
Can a non-Rust process participate?
No. The protocol is defined by Rust types serialized with Serde, so both processes must be written in Rust.
Related pages
The timer daemon example includes the complete Unix and Windows code. The smaller counter example introduces remote trait calls over TCP. For more notification patterns, server push and callbacks covers the patterns, and channels between processes explains the underlying channel model.