Use case

Chat with no server.

Two people join the same topic, find each other and talk over a direct, encrypted connection. The bootstrap node only helps them meet; the messages never pass through it.

The pieces

The libraries this needs. Each has install steps and a compiled example in its docs.

How it goes

  1. Each peer gets a key

    A peer is an Ed25519 key from kolo-id. Its public key is its name on the network; there are no accounts.

  2. Both join a topic

    A topic is 32 bytes they agree on, such as a hash of a room name. kolo-swarm announces it and looks it up through a bootstrap node.

  3. They talk directly

    The connection is a kolo-secure channel tied to both keys, so each side knows whose key it is talking to. Each message travels on its own stream.

Join a topic in a few lines.

One function starts a swarm, bootstraps it from the nodes you pass in and joins a topic. Then it prints the start of the public key of every peer that connects.

  • Line 04Start a swarm with your identity.
  • Line 09Bootstrap from nodes you choose.
  • Line 15Announce the topic and look up its peers.
  • Line 19Read each new connection as an event.
  • Go
main.go
func join(
    ctx context.Context, id *koloid.Identity, nodes []netip.AddrPort,
) error {
    sw, err := koloswarm.New(ctx, "127.0.0.1:0", id, nil)
    if err != nil {
        return err
    }
    defer sw.Close()
    if err := sw.Bootstrap(ctx, nodes); err != nil {
        return err
    }

    topic := koloswarm.Topic(sha256.Sum256([]byte("my first topic")))
    opts := &koloswarm.JoinOptions{Announce: true, Lookup: true}
    if err := sw.Join(ctx, topic, opts); err != nil {
        return err
    }
    for {
        ev, err := sw.Next(ctx)
        if err != nil {
            return err
        }
        if ev.Kind == koloswarm.Connected {
            fmt.Printf("connected to %x\n", ev.Peer.PublicKey()[:8])
        }
    }
}
go get github.com/ciranda-dev/kolo-swarmGo 1.26+ · 27 lines

Good to know

A topic is not a secret: anyone who knows the 32 bytes can join it. Decide who may read what with keys, not with the topic. Treat what you receive as untrusted: the docs example caps each message and drops control characters.

Next