WeaveDocs
Weave Swarm

Topics

Topic membership in Weave Swarm — join, leave, and reconcile topic-scoped peer sets with stable topic IDs.

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::{JoinOptions, Swarm, SwarmConfig, TopicId};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // TopicId is a 32-byte tag — derive one from any string via from_str(),
    // or from arbitrary bytes for binary topics.
    let topic = TopicId::from_str("woven://dsocial/global");
    let server_only = JoinOptions::server_only();
    let _cap_capped = JoinOptions::with_max_peers(64);

    // Swarm::join enters the topic and updates the DHT advertisement; leave()
    // removes the subscription cleanly. topic_count() reports the current size.
    let swarm = Swarm::new(SwarmConfig::default()).await?;
    swarm.join(topic.clone(), server_only).await?;
    let active_before = swarm.topic_count();
    swarm.leave(&topic).await?;
    let active_after = swarm.topic_count();

    println!(
        "topic={} active_before={} active_after={}",
        hex::encode(topic.as_bytes()),
        active_before,
        active_after,
    );
    Ok(())
}