WeaveDocs
Weave Swarm

Connections

Connection state machine for Weave Swarm — track peer connections, retries, and lifecycle transitions per topic.

Purpose

Topic swarm for peer IDs, topic IDs, join/update options, events, priorities, connection state, and metrics.

This page follows the real source shape for Weave Swarm and explains the workflow a developer is likely to use first.

Developer workflow

Start from the smallest constructor or builder, perform one meaningful operation, inspect the returned state, then add the relevant policy, storage, or network integration. The examples below should be expanded whenever the crate API changes.

Primary types to know

  • BackoffConfig — network/weave-swarm/src/config.rs
  • Connection — network/weave-swarm/src/connection.rs
  • ConnectionStats — network/weave-swarm/src/connection.rs
  • JoinOptions — network/weave-swarm/src/topic.rs
  • LimitsConfig — network/weave-swarm/src/config.rs
  • NatConfig — network/weave-swarm/src/config.rs
  • PeerId — network/weave-swarm/src/connection.rs
  • Priority — network/weave-swarm/src/priority.rs
  • Swarm — network/weave-swarm/src/swarm.rs
  • SwarmConfig — network/weave-swarm/src/config.rs
  • SwarmMetrics — network/weave-swarm/src/metrics.rs
  • TelemetryConfig — network/weave-swarm/src/config.rs

Example shape

use weave_swarm::{PeerId, Swarm, SwarmConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // A Swarm owns the dial/accept logic plus a DashMap of live Connections.
    let swarm = Swarm::new(SwarmConfig::default()).await?;

    // Inspect the per-process peer identity and the current connection table.
    let local = swarm.local_peer_id();
    let connections = swarm.connections();

    // broadcast() fans a frame to every live connection and returns how many
    // recipients accepted the write. Use send_to_peer for unicast.
    let target = PeerId::from_public_key(&[1u8; 32]);
    if let Err(err) = swarm.send_to_peer(&target, b"unicast hello").await {
        eprintln!("not yet connected: {err}");
    }
    let fanout = swarm.broadcast(b"global hello").await.unwrap_or(0);

    println!(
        "local={} live_connections={} broadcast_count={}",
        hex::encode(local.as_bytes()),
        connections.len(),
        fanout,
    );
    Ok(())
}