Hi,
I am new to libp2p and I am trying to learn how to use it, I was able to connect two nodes with
bootstrap, now I was trying to send a message to each other, I saw examples like echo and chat, I have run echo in my computer with no problem, but when I get to my code I can’t get it to work, beucase I receive a error like this:
Connection established to: QmSY1XoXy6vH44RQziyTSNmf99dk5vyC9tQ6PU49EgqigU /ip4/127.0.0.1/tcp/35071/p2p/QmSY1XoXy6vH44RQziyTSNmf99dk5vyC9tQ6PU49EgqigU
Connection established to: QmSY1XoXy6vH44RQziyTSNmf99dk5vyC9tQ6PU49EgqigU /ip4/127.0.0.1/tcp/35071/p2p/QmSY1XoXy6vH44RQziyTSNmf99dk5vyC9tQ6PU49EgqigU
file:///media/fsvieira/Data/fsvieira/sandbox/libp2p-repo/test/node_modules/uint8arraylist/dist/src/index.js:25
length += buf.length;
^
TypeError: Cannot read properties of undefined (reading 'length')
at Uint8ArrayList.appendAll (file:///media/fsvieira/Data/fsvieira/sandbox/libp2p-repo/test/node_modules/uint8arraylist/dist/src/index.js:25:31)
at Uint8ArrayList.subarray (file:///media/fsvieira/Data/fsvieira/sandbox/libp2p-repo/test/node_modules/uint8arraylist/dist/src/index.js:75:14)
at Object.sink (file:///media/fsvieira/Data/fsvieira/sandbox/libp2p-repo/test/node_modules/libp2p-mplex/dist/src/stream.js:92:82)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ERR_UNSUPPORTED_PROTOCOL'
}
And my code is like this:
import Libp2p from 'libp2p'
import { NOISE } from 'libp2p-noise'
import { Mplex } from 'libp2p-mplex'
import Bootstrap from 'libp2p-bootstrap'
import TCP from 'libp2p-tcp'
import {pipe} from 'it-pipe'
async function main (bootstrap) {
const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP],
connEncryption: [NOISE],
streamMuxer: [Mplex],
peerDiscovery: [ Bootstrap ]
},
config: {
peerDiscovery: {
[Bootstrap.tag]: {
interval: 60e3,
enabled: bootstrap.length > 0,
list: bootstrap
}
}
}
})
await node.handle('/chat/1.0.0', async ({ stream }) => {
// Send stdin to the stream
// stdinToStream(stream)
// Read the stream and output to console
// streamToConsole(stream)
console.log('received something!');
})
// start libp2p
await node.start()
console.log('libp2p has started')
const listenAddrs = node.transportManager.getAddrs()
console.log('libp2p is listening on the following addresses: ', listenAddrs)
const advertiseAddrs = node.multiaddrs
console.log('libp2p is advertising the following addresses: ', advertiseAddrs)
const peerId = node.peerId.toB58String();
console.log("My ID ", peerId);
console.log(node.multiaddrs.map(address => `${address}/p2p/${peerId}`));
node.connectionManager.on('peer:connect', async (connection) => {
console.log('Connection established to:', connection.remotePeer.toB58String(), connection.remoteAddr.toString()) // Emitted when a peer has been found
if (bootstrap.length) {
const { stream } = await node.dialProtocol(connection.remoteAddr.toString() /*bootstrap[0]*/, '/chat/1.0.0')
pipe(
// Source data
['hey'],
// Write to the stream, and pass its output to the next function
stream,
// Sink function
async function (source) {
// For each chunk of data
for await (const data of source) {
// Output the data
console.log('received echo:', data.toString())
}
}
)
console.log('finish', result);
}
})
node.on('peer:discovery', (peer) => console.log('Discovered: ', peerId.toB58String()))
// stop libp2p
/*
await node.stop()
console.log('libp2p has stopped')*/
}
console.log(process.argv);
const [n, p, ...bootstrap] = process.argv;
main(bootstrap);
So I run the code with no args (no peers to bootstrap) and then I run the second one with the address of the first, they connect ok, but when they are connected I try to send a message but I get an error.