Categorygithub.com/libp2p/go-libp2p-swarm
modulepackage
0.11.0
Repository: https://github.com/libp2p/go-libp2p-swarm.git
Documentation: pkg.go.dev

# README

DEPRECATION NOTICE

This package has moved into go-libp2p as a sub-package, github.com/libp2p/go-libp2p/p2p/net/swarm.

go-libp2p-swarm

==================

Go Reference Code Coverage Discourse posts

The libp2p swarm manages groups of connections to peers, and handles incoming and outgoing streams.

The libp2p swarm is the 'low level' interface for working with a given libp2p network. It gives you more fine grained control over various aspects of the system. Most applications don't need this level of access, so the Swarm is generally wrapped in a Host abstraction that provides a more friendly interface. See the host interface for more info on that.

Table of Contents

Install

go get github.com/libp2p/go-libp2p-swarm

Usage

Creating a swarm

To construct a swarm, you'll be calling NewSwarm. That function looks like this:

swarm, err := NewSwarm(peerID, peerstore)

The first parameter of the swarm constructor is an identity in the form of a peer.ID.

The second argument is a peerstore. This is essentially a database that the swarm will use to store peer IDs, addresses, public keys, protocol preferences and more.

Streams

The swarm is designed around using multiplexed streams to communicate with other peers. When working with a swarm, you will want to set a function to handle incoming streams from your peers:

swrm.SetStreamHandler(func(s network.Stream) {
	defer s.Close()
	fmt.Println("Got a stream from: ", s.SwarmConn().RemotePeer())
	fmt.Fprintln(s, "Hello Friend!")
})

Tip: Always make sure to close streams when you're done with them.

Contribute

PRs are welcome!

Small note: If editing the Readme, please conform to the standard-readme specification.

License

MIT © Jeromy Johnson


The last gx published version of this module was: 3.0.35: QmQVoMEL1CxrVusTSUdYsiJXVBnvSqNUpBsGybkwSfksEF

# Packages

Deprecated: This package has moved into go-libp2p as a sub-package: github.com/libp2p/go-libp2p/p2p/net/swarm/testing.

# Functions

NewSwarm constructs a Swarm.
WithConnectionGater sets a connection gater Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.WithConnectionGater instead.
Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.WithDialTimeout instead.
Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.WithDialTimeoutLocal instead.
WithMetrics sets a metrics reporter Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.WithMetrics instead.
Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.WithResourceManager instead.

# Constants

ConcurrentFdDials is the number of concurrent outbound dials over transports that consume file descriptors Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.ConcurrentFdDials instead.
DefaultPerPeerRateLimit is the number of concurrent outbound dials to make per peer Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.DefaultPerPeerRateLimit instead.
DialAttempts governs how many times a goroutine will try to dial a given peer.

# Variables

BackoffBase is the base amount of time to backoff (default: 5s).
BackoffCoef is the backoff coefficient (default: 1s).
BackoffMax is the maximum backoff time (default: 5m).
ErrAddrFiltered is returned when trying to register a connection to a filtered address.
ErrAllDialsFailed is returned when connecting to a peer has ultimately failed Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.ErrAllDialsFailed instead.
ErrConnClosed is returned when operating on a closed connection.
ErrDialBackoff is returned by the backoff code when a given peer has been dialed too frequently Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.ErrDialBackoff instead.
ErrDialTimeout is returned when one a dial times out due to the global timeout Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.ErrDialTimeout instead.
ErrDialToSelf is returned if we attempt to dial our own peer Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.ErrDialToSelf instead.
ErrGaterDisallowedConnection is returned when the gater prevents us from forming a connection with a peer.
ErrNoAddresses is returned when we fail to find any addresses for a peer we're trying to dial.
ErrNoGoodAddresses is returned when we find addresses for a peer but can't use any of them.
ErrNoTransport is returned when we don't know a transport for the given multiaddr.
ErrSwarmClosed is returned when one attempts to operate on a closed swarm.

# Type aliases

Conn is the connection type used by swarm.
DialBackoff is a type for tracking peer dial backoffs.
DialError is the error type returned when dialing.
Deprecated: use github.com/libp2p/go-libp2p/p2p/net/swarm.Option instead.
Stream is the stream type used by swarm.
Swarm is a connection muxer, allowing connections to other peers to be opened and closed, while still using the same Chan for all communication.
TransportError is the error returned when dialing a specific address.