Home > Engineering > Infrastructure > P2P building blocks > Hyperswarm

Hyperswarm

Hyperswarm is the layer most apps actually import: topic-based discovery plus connection lifecycle management on top of HyperDHT. You join 32-byte topics; it finds peers, dedups and reconnects, lets you ban misbehavers, and hands you Noise-encrypted connections. HyperDHT is the engine underneath; Hyperswarm is the ergonomic wrapper.


Core API

See the README for exhaustive detail.

Create

Join / leave

Connections

Lifecycle

PeerDiscovery (returned by join)

PeerInfo


Gotchas

topic is a 32-byte Buffer (typically hash(name)), not an arbitrary string — pass the wrong shape and discovery silently does nothing useful.

swarm.flush() and discovery.flushed() are different things, and confusing them is the classic “no peers found” bug. swarm.flush() waits for in-flight operations to settle; discovery.flushed() waits until you are actually announced on the DHT (server mode), which is what a remote peer needs before it can find you. Precise announce→lookup propagation timing is a POC item — don’t assume it’s instant.

join defaults to both client and server. For a pure dialer, pass { client: true, server: false }.

ban() / leavePeer() stop reconnection but do not close existing connections.

firewall is a synchronous remotePublicKey => boolean. The connection socket is already encrypted — don’t wrap it again.


Version

hyperswarm@4 (current latest is the 4.x line). A stray tmp dist-tag exists upstream — ignore it; latest is the install target. Check the npm registry before pinning.

Sources