I have libp2p node
This node connects to the relay and has the ability to connect to a remote user.
this.libp2p = await createLibp2p({
peerId: peerObject.PeerId,
peerStore: PersistentPeerStore,
addresses: {
listen: [
'/webrtc-direct',
'/webrtc'
]
},
transports: [
webRTCDirect(),
webSockets({
filter: filters.all
}),
webRTC(),
circuitRelayTransport({
discoverRelays: 2
})
],
peerDiscovery: boot,
connectionEncryption: [noise()],
streamMuxers: [yamux()],
services: {
identify: identify(),
identifyPush: identifyPush(),
pubsub: gossipsub(),
dcutr: dcutr(),
ping: ping(),
dht: isDht ? kadDHT({
kBucketSize: 12,
kBucketSplitThreshold: `kBucketSize`,
prefixLength: 32,
clientMode: false,
querySelfInterval: 5000,
initialQuerySelfInterval: 1000,
allowQueryWithZeroPeers: false,
protocol: DhtProtocol,
logPrefix: "libp2p:kad-dht",
pingTimeout: 10000,
pingConcurrency: 10,
maxInboundStreams: 32,
maxOutboundStreams: 64,
peerInfoMapper: publicAddressesMapper,
}) : () => {
}
},
connectionManager: {
minConnections: 20
},
transportManager: {
faultTolerance: FaultTolerance.NO_FATAL
},
connectionGater: {
denyDialPeer: (currentPeerId) => {
return false
},
}
})
For transet text data.
Add listener for streams
await this.libp2p.handle(protoText, self.handler);
await this.libp2p.handle(protoAudio, self.handler);
handler
There I accept incoming data.
handler: {
value: async function ({ connection, stream }) {
const protocol = stream.protocol
if(stream.protocol === protoText) {
const lp = lpStream(stream)
const res = await lp.read()
const output = new TextDecoder().decode(res.subarray())
this._message = output
this.printSmbl()
}
if(stream.protocol === protoAudio) {
console.log('----------- stream --------------', '???')
}
},
writable: true
},
sender
Here I send data text and audio.
And I can’t figure out what to put here correctly.
It’s clear with the text, but not how to make an audio stream.
if(type === 'text') {
const signal = AbortSignal.timeout(5000)
this.DOM.input.textContent = ''
const stream = await self.libp2p.dialProtocol(ma, proto, {
signal
});
const lp = lpStream(stream)
await lp.write(new TextEncoder().encode(msg))
}
if(type === 'audio') {
const signal = AbortSignal.timeout(5000)
this.DOM.input.textContent = ''
const stream = await self.libp2p.dialProtocol(ma, protoAudio, {
signal
});
????
return true
}
I can’t stream audio remotely.
I can’t find examples of how to transfer a audio stream.
I found examples of remote connection for webrtc but not for the libp2p library.