FAQs about libp2p

What is libp2p all about?

libp2p is a toolkit for developing peer-to-peer network applications in a growing variety of languages.

The docs site has a What is libp2p section that gives a high-level overview of the problems libp2p was built to solve.

What’s the status of X libp2p feature for my favorite language?

The implementations section of libp2p.io has an overview of the functionality supported by each language implementation. However, some new implementations (e.g. python) are not yet tracked on that page.

For the most up-to-date picture, it’s best to visit the main repo of the implementation:

How do libp2p peers communicate?

When peers interact with each other, each side gets a bidirectional stream that they can read from and write to. From the perspective of a libp2p application, this is a fairly simple pipe that can be used with the language’s native IO facilities. For example, the go implementation extends the standard go io.Reader and io.Writer interfaces.

Under the hood, libp2p’s switch (the “dialer machine” that manages connection state) will establish a transport connection (e.g. a TCP stream), negotiate an an encryption mechanism and stream multiplexer with the remote peer, and open a new multiplexed stream for the application to use. If there’s already an open connection the the remote peer, the switch will just open a new multiplexed stream over the existing connection.

The semantics of what gets sent over the libp2p stream is determined by the application protocols in use. Each libp2p stream is “tagged” with a protocol id, which is a unique string containing a name and version, e.g. /ipfs/id/1.0.0. When new streams are opened, the peers engage in a protocol negotiation process using multistream-select to agree on a supported protocol. Once agreement is reached, the protocol determines what gets sent over the wire.

How do I develop my own application protocols?

libp2p makes very few assumptions about the structure of the data your application wants to send over the wire. As such, there’s no “canonical” or recommended serialization or RPC format.

The protocols used internally by libp2p (e.g. the identify protocol) send messages encoded as protobufs over the wire using a simple length-prefixed encoding, where the size of the message is sent, followed by the serialized protobuf. For details, see go-msgio, which implements the length-prefixed reading and writing in go.

How do peers discover each other?

Local peers can find each other using multicast DNS, if enabled and supported by their libp2p implementation.

libp2p also provides a Kademlia-inspired distributed hash table, which allows peers to discover each other in a distributed fashion.

Can I run libp2p apps in the browser?

Yes! js-libp2p supports browser runtimes, although you are limited to transports supported by browsers, e.g. websockets and WebRTC. Indirect support for other transports can be provided by using a circuit relay peer to act as a “bridge” between the browser and peers using other transports (e.g. TCP).

3 Likes

Everyone feel free to propose new Q’s for this thing! These are just the ones that are in my head at the moment.

Hello, Can we use libp2p in kotlin/java for developing android apps? or maybe call go functions from kotlin/java?

Right now the best way to use libp2p from java is usually to run the libp2p daemon alongside your java app, but that’s likely more difficult on Android than a traditional desktop OS. I don’t have experience integrating go code with the Android SDK, but it does seem possible.

So unfortunately we haven’t got a ready-to-use solution for this that I’m aware of. If you do end up pursuing the go / Android integration, I’d love to hear about it!

Thanks @yusef, I’ll try to make a minimal app and see what will happen :slight_smile:
How about React Native which is built on top of JavaScript? Do you have any experience/idea?

There’s some discussion about js-libp2p in mobile at https://github.com/libp2p/js-libp2p/issues/89#issuecomment-482559954. While the issue is around Cordova/Phonegap, it’s also pertinent to React Native. In theory it should function, but a Transport bridge would likely need to be developed to allow js to use the mobile OS’s networking layer.

1 Like

Thanks @jacobheun , I’m not familiar with Cordova/Phonegap at all but React Native supports WebSocket, and I saw this protocol is supported by libp2p, Do you think it can be used to make connection?

I don’t know of anyone that’s attempted to get that working, but a minimal libp2p config with the websockets transport should be able to connect to the bootstrap nodes. I’d be interested in hearing about issues getting that running. If you plan to give it a try, let me know if you hit any issues, I’d love to see that working.

JFI, React Native is not just a webview like Cordova, and UI elements will be replaced by native UIs in OS (Android/IOS)…
I’m building an app, using React Native and “nodejs-mobile-react-native”, will let you know how’s it going… Thanks

Hello @jacobheun, Have you checked https://github.com/ligi/IPFSDroid which is based on https://github.com/ligi/ipfs-api-kotlin ?
Can this library be helpful in anyway?

It looks like https://github.com/ligi/ipfs-api-kotlin is just using the ipfs http api to access a remote node. Getting a node running locally on the device would be ideal.

What’s the lifecycle of a peer Connection?

Going to try and answer my own question, because I just spend some time trying to figure this out today. I believe it’s the following:

  1. Peer is discovered
  2. Peer is manually or automatically dialed (no protocol is established)
  3. Peer is connected, but for data exchange to happen, a protocol should be dialed via dialProtocol. Dialing a peer results in a “logical” Connection that can then be used to stream data back and forth via pull-streams.
  4. Peer is disconnected and real connection is lost when either Peer disconnects or calls hangUp

The Connection in step 3 doesn’t really create a new connection, instead it just creates an easy to handle stream that is focused on a specific task designated by the protocol design.


Please correct me or help me if there is a better way to explain that. It might help to have an example.

@JustMaier that is correct. The API isn’t very clear in JS and we’ll be working on that in the coming months. dialProtocol is a combination of what should really be two separate methods: connect and newStream. The connect is the establishment of the basic connection, which will include encryption and multiplexing setup. From there, as long as a connection exists to that peer, dialProtocol will simply create a new stream on that connection for the given protocol. The result is a Connection which is a pull stream and can be managed with the pull-stream libraries.

There is a WIP spec at https://github.com/libp2p/specs/pull/168 for documenting connection upgrade logic, but this will be implementation agnostic. We’ll be improving the JS docs as we improve the api in the coming months.

2 Likes

How can we share a file between the peers? I can see the examples for chat application, and discovery of nodes but don’t see the how do we share the files between nodes.

@tirupatiblibp2p what language of libp2p are you looking to use? IPFS is an option, but if you want to use libp2p strictly you’ll need to create a protocol for doing this such as /file/1.0.0. After registering the handler you can connect to your desired peer and open a new stream for your protocol. From there you should be able to stream the file over.

Hey everyone! In case anyone is interested in C++ implementation take a look on our repo https://github.com/soramitsu/libp2p. @yusef is it possible to update pinned message and add our implementation there as well?

Hey @kamilsa! It’s awesome to see the C++ libp2p coming together and available outside of kagome :slight_smile: I just added a link in the pinned post. Nice work!

I’m still confused: is a libp2p network global and shared across applications (meaning two applications can pubsub to same topics, or share data about peers, and so on), or does each application use a separate libp2p network (meaning two applications would need a bridge to share topics and peers)?

Hi @yusef ,

Since you mentioned

libp2p also provides a Kademlia-inspired distributed hash table, which allows peers to discover each other in a distributed fashion.

can I assume that a p2p network based on libp2p is essentially a Kademlia network?