Sharing a TCP connection between independent conversations

A TCP or TLS connection provides a single byte stream. As soon as two independent conversations share it, for example commands and a bulk file transfer, or requests and server-initiated events, your application needs message framing, tags to route each message to its handler, and care that a large transfer does not starve everything queued behind it. Opening a separate connection for each conversation avoids this, but multiplies handshakes and TLS sessions, causes trouble with firewalls and NATs, and the connections no longer fail together.

Compared with yamux and QUIC: These stream multiplexers solve one half of the problem by providing many byte streams over one connection. However, the streams still carry raw bytes, so serialization, framing and matching a stream to its purpose remain your job.

Typed channel multiplexing vs. yamux byte streams

Remoc multiplexes at the level that Rust programs already use internally: channels. A connection starts with a base channel. To open a further channel, you just send one of its endpoints inside a message, like you would hand a Tokio channel endpoint to another task. There is no port to open, no stream number to agree on and no registry to consult; the endpoint simply arrives where it is needed.

Choose a byte-stream multiplexer such as yamux when each logical stream must carry an existing byte-oriented protocol. Choose Remoc when both peers are Rust programs and each conversation should carry Serde values, channel endpoints or trait calls. Remoc adds its own protocol; it is not wire-compatible with yamux and does not replace TCP with a new transport.

Each channel is typed and carries values of a single Serde type, serialized with the Postbag codec, so the compiler checks what may be sent over it. Every channel has its own flow control, meaning that a receiver which stops reading only stalls its own sender and does not affect unrelated traffic. Large messages are transmitted in chunks to avoid one channel blocking another. Remoc also provides remote functions, RPC on traits and observable collections over the same connection.

Sending a new channel over an existing TCP connection

In the counting example, a request carries the sender of a new mpsc channel. The server replies through that sender.

A channel inside a message
#[derive(Serialize, Deserialize)]
struct CountReq {
    up_to: u32,
    // A sender, on its way to the peer.
    seq_tx: rch::mpsc::Sender<u32>,
}
  1. The client creates an mpsc channel and keeps its receiver.
  2. It sends CountReq, carrying the sender, over the connection's base channel.
  3. The server uses seq_tx to send numbers; the client reads them from its receiver.

Both channels share one TCP connection; no second socket is opened for the sequence. For RPC and multiple update channels sharing a connection, the counter example returns both a watch receiver and an mpsc receiver.

Complete channels example → How sendable channels work →

Questions

How is this different from yamux or QUIC streams?

Stream multiplexers such as yamux and QUIC provide additional byte streams, so serialization, framing and dispatch remain the job of the application. Remoc multiplexes typed channels instead. Each channel carries values of one Serde type, and remote functions, trait calls and observable collections build on the same connection.

Does a slow channel block the others?

No. Each channel has its own flow control, so a receiver that stops reading only stalls its own sender. The channels still share the underlying transport and therefore its total bandwidth.

Which transports can carry the multiplexed connection?

Any ordered byte or packet stream can be used, for example TCP, TLS, a QUIC stream, a WebSocket, a UNIX pipe or a serial link. Remoc's channel multiplexer runs on top of it and does not depend on a particular transport type.

Do both ends have to be written in Rust?

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

How many channels can one connection carry?

Channels are lightweight compared with separate TCP connections because they share the existing transport. Opening one sends its endpoint inside a message, without a port, transport handshake or registration. A channel per request or subscriber does not require additional network connections.

Related pages

Channels between processes and machines shows a complete channel exchange, and the Remoc overview covers remote trait calls and pipelining. Remoc vs. other RPC libraries compares it with tarpc, gRPC and Cap'n Proto, and the benchmarks compare a Remoc channel with serializing directly to plain TCP.