How do I send an arbitrary message from a js-ipfs node to another js-ipfs node by peer ID? It is my understanding that logically js-ipfs must have within it somewhere the capacity to send a message to a peer based on their peer ID ('QmABCDEtc'), something like a standard XMLHttpRequest but using a multiaddr/peerID instead of a domain name/ip address. This function doesn't appear to be exposed through the public API, unless I'm missing something. As per the readme, Every IPFS instance also exposes the libp2p API at ipfs.libp2p, and my next thought would be that there'd be something exposed there but as far as I can see there's only 'ipfs.libp2p.start' and 'ipfs.libp2p.stop'. Am I missing something, or is it not possible?
You can't send 'any arbitrary message', but you can prepare your js-ipfs nodes to receive the arbitrary messages you want to send.
You will need to use the libp2p instance inside js-ipfs, learn how with this simple example: https://github.com/ipfs/js-libp2p-ipfs-nodejs/tree/master/examples/echo
@reit-c the new libp2p tutorials should answer all your questions: https://github.com/libp2p/js-libp2p/tree/master/examples
Let me know if you have any questions :) Closing this issue for now.
Is it possible to do this from the browser via <script src="https://unpkg.com/ipfs/dist/index.js"></script>, _instead of_ from node.js? Which means you wouldn't be able to do things like
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Railing = require('libp2p-railing')
const spdy = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
etc, would it be possible to get to the libp2p code from that direction anyway?
Another question, I've looked over the echo and chat examples, and they both appear to be addressing the other node by IP and port - not public key. Is that a limitation of libp2p or is it also capable of addressing nodes by '/ipns/Qmabcdefghijklmnop123456789'?
@reit-c absolutely, check https://github.com/libp2p/js-libp2p/tree/master/examples/libp2p-in-the-browser :)
Hey @daviddias,
I was working on the chat example in the link: [ https://github.com/libp2p/js-libp2p/tree/master/examples]
When I ran the scripts listener and dialer it worked fine.
But when I changed the nodes: from libp2p nodes to IPFS nodes.
I got this error
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'sink' of undefined
I will tell the steps where my code is different from the example:
I think the error is here because when i printed stream it said undefined.
let mypeers = await nodeDialer.dht.findProvs(added[0].hash)
const { stream } = await nodeDialer.libp2p.dialProtocol( mypeers[mypeers.length - 1] ,'/chat/1.0.0')
console.log(stream)
But in the actual example it printed
{
close: [Function: close],
abort: [Function: abort],
reset: [Function: reset],
sink: [Function: sink],
source: Object [AsyncGenerator] {},
timeline: { open: 1580804129316, close: null },
id: 'i1'
}
What am I doing wrong?
@kuabhish please install ipfs v0.41.0-rc.0 which has the new version of libp2p that the example is referring to.
How can I install ipfs v0.41.0-rc.0 @alanshaw ?
$ npm install [email protected]