libp2p / js-libp2p

The JavaScript Implementation of libp2p networking stack.
https://libp2p.github.io/js-libp2p/
Other
2.33k stars 445 forks source link

Can libp2p.dial be used to send arbitrary messages? #267

Closed klueq closed 2 years ago

klueq commented 6 years ago

Is it possible to use this API to send arbitrary messages to a chosen peer id?

jacobheun commented 6 years ago

In order to send/receive messages the libp2p nodes need to use a specific protocol. This allows them each to register a handler for that protocol so they know how to process the messages. In that chat example, https://github.com/libp2p/js-libp2p/blob/master/examples/chat/src/listener.js#L30, the handler is for text. You can make the handler do anything for your specific protocol, including the handling of arbitrary messages, but a protocol is needed to perform communication.

Is there something more specific you're trying to do that we can help with?

klueq commented 6 years ago

I want to implement a basic idea of a "distributed shared folder" where each user keeps a copy, maybe partial, of the folder and they sync the list of files between each other. In more detail:

https://github.com/klueq/shared-folder-ipfs/blob/master/docs/sync.md

The chat example looks very promising. How efficient is this mechanism compared to sending a UDP message, if we could do that in JS? For example, if two nodes want to sync their lists of files and they need to exchange with 20 messages, how much time will it take?

klueq commented 6 years ago

How do I connect to a peer by its id?

  let peer = 'QmXcBNGp2SBzbogsbEnHxdrokw6te9y2RU9rKYD1sQc1km';
  ipfs._libp2pNode.dialProtocol(peer, '/chat/1.0.0' (...args) => {
    console.log(...args);
  });

It returns a Error: multiaddr "QmXcBNGp2SBzbogsbEnHxdrokw6te9y2RU9rKYD1sQc1km" must start with a "/".

klueq commented 6 years ago

I've figured out this part: the peer argument needs to be one of the values from libp2p.peerInfo.multiaddrs.toArray() on the remote peer.

Now I'm trying to send a message thru this esoteric Connection object. It doesn't seem to have a simple send method, but is rather meant to be used within the pull/pushable/stream/etc. wizardry. Is there a way to avoid all that setup and just send a message?

It looks like the pull/pushable stuff is an attempt to mimic the stream >> and << operators from C++.

jacobheun commented 6 years ago

You can also just use the PeerInfo object, libp2p.peerInfo, of the other node. That way the switch will try all available addresses instead of just one.

We don't currently expose any interface to do a send. We are looking at improving the interface soon to make things easier. The biggest issue with adding a send method is that it starts making assumptions about the protocol being used, or a default send protocol would need to be added. What the application needs to do with the sent message is also problematic.

It might be helpful to at least add some example methods or classes extending libp2p to help with bootstrapping applications.

klueq commented 6 years ago

How can I get a PeerInfo object for a peer id that I got over email, for instance? I'd need to do a lookup with ipfs dht findpeers <peed-id>, right?

I understand that the p2p send/recv API isn't public and can change at any moment. But for the lack of other options, I'd rather use the undocumented API than try to set up by own WebRTC DataChannel, which would involve complicated signaling, ICE/NAT/STUN/TURN mess and so on.

jacobheun commented 6 years ago

Yes, if you have just the peer id string you'd need to query the dht. ipfs dht findpeer will query the libp2p peer routers. If someone sends you their addresses from ipfs id then you could dial those directly.

You really dont need to mess with the internals to do straight sends, but right now a simple method would require creating a custom node with some helper methods and your custom protocol handlers. If you want to extend libp2p to do that within js-ipfs, you could extend the libp2p class then use that custom libp2p within js-ipfs https://github.com/ipfs/js-ipfs/tree/master/examples/custom-libp2p.

achingbrain commented 2 years ago

Closing due to staleness. Please ask further "how do I"-type questions on https://discuss.libp2p.io/