Skip to main content

Mesh Routing

Mesh routing enables multi-hop message delivery through trusted peers. When two devices cannot connect directly, messages are routed through intermediate peers that both can reach.

When to Use Mesh Routing

Mesh routing is useful when you have 3 or more devices and some cannot reach each other directly. Instead of relying on a central relay server, messages are forwarded through trusted peers.

Example scenario:

The laptop and desktop cannot connect directly (e.g., different networks, strict NAT), but both can reach the phone. With mesh routing enabled, the phone automatically relays traffic between them -- all end-to-end encrypted.

Enabling Mesh Mode

Enable mesh routing when creating a node:

use cairn_p2p::{CairnConfig, create};

let config = CairnConfig {
mesh_enabled: true,
..CairnConfig::default()
};
let node = create(config)?;
node.start().await?;

Topology and Routing Behavior

  • Automatic route discovery: Mesh peers automatically discover multi-hop routes. No manual configuration of routing tables is needed.
  • End-to-end encryption: Traffic is encrypted between the source and destination. Relay peers forward ciphertext and cannot read message content.
  • Dynamic routing table: The routing table is maintained automatically as peers join, leave, or change connectivity. Routes adapt to network changes in real time.

Use Case: Multi-Device File Sync Through a Hub

A common pattern is to designate one device as an always-on hub (using server mode) while other devices sync through it.

The hub:

  • Relays messages between devices that are not directly connected
  • Stores messages for offline devices (store-and-forward via server mode)
  • Syncs data across all connected devices automatically
// Hub node: server mode + mesh
let config = CairnConfig {
server_mode: true,
mesh_enabled: true,
..CairnConfig::default()
};
let hub = create(config)?;
hub.start().await?;

// Client node: mesh enabled, connects to hub
let client_config = CairnConfig {
mesh_enabled: true,
..CairnConfig::default()
};
let client = create(client_config)?;
client.start().await?;
let session = client.connect(&hub_peer_id).await?;
session.send("sync", b"file update").await?;

With this setup, any device can send a message to any other device -- the hub automatically routes it through the mesh, even if the sender and receiver have never connected directly.