Hi,
I’ve been at this before but i’m taking a bit of a different approach this time. For those who remember it, when i asked about this before i was trying to get IPFS’s PubSub mechanism working on websites which brought me the webrtc-star nightmare and all together just not a concept that seemed stable enough to use.
The approach i’m taking now is simply making a socket.io proxy endpoint nodes to which users can connect. You can find the code for that here. That however is still based on a full IPFS node running. For cheap $5 servers that’s a bit taxing. So i’m trying to accomplish the same with only libp2p.
As a “simple” proof of concept if i can receive pubsub data i try the following code (99% copy/paste from examples, please do tell me if i do something totally wrong):
'use strict'
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const Bootstrap = require('libp2p-bootstrap')
const DHT = require('libp2p-kad-dht')
const Gossipsub = require('libp2p-gossipsub')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')
const bootstrapers = [
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
]
;(async () => {
const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [NOISE],
peerDiscovery: [Bootstrap],
dht: DHT,
pubsub: Gossipsub
},
config: {
peerDiscovery: {
bootstrap: {
interval: 60e3,
enabled: true,
list: bootstrapers,
autoDial: true
}
},
dht: { // The DHT options (and defaults) can be found in its documentation
kBucketSize: 20,
enabled: true,
randomWalk: {
enabled: true, // Allows to disable discovery (enabled by default)
}
}
}
})
const topic = 'news'
await node.start()
node.pubsub.on(topic, (msg) => {
console.log(`node received: ${uint8ArrayToString(msg.data)}`)
})
await node.pubsub.subscribe(topic)
})();
The annoying thing here is that this works, but not immediately.
It’s my suspicion that the bootstrap nodes don’t have pubsub enabled.
With DHT i’m getting a lot more node connections. After, say, 2 minutes or so i finally have a connection to a node with pubsub enabled after which the above code receives pubsub messages just fine.
So this works That’s a good thing!
I’m wondering if i can somehow instruct this code to make a connection to pubsub enabled nodes and drop the other connections?
What i did try is add a bootstrap node with pubsub enabled and that worked too.
I also tried adding the bootstrap nodes from IPFS, thinking they “surely” had pubsub enabled… Turns out that apparently isn’t the case either. That does kinda make me wonder how enabled pubsub is on the IPFS network if their own bootstrap nodes apparently don’t have it enabled.
Lastly, how can i dial a node by only it’s CID? I have a few nodes where i know pubsub is running, but i don’t want to add them in the bootstrap list because their IP is dynamic and could therefore change.
Cheers,
Mark
Edit.
In fact, i want to spin up half a dozen of these IPFS nodes handling pubsub. How can i make them connect to each other without specifying them in the bootstrap? Would they find each other via DHT in the above code? Also, which ports do i need to open for that? For IPFS that’s 4001, is that the same for libp2p?
Edit 2.
I do notice each instance of the above code is creating a new peerid. Probably because there isn’t a keychain? Is there an example somewhere to setup libp2p with keychain?
cc ping @vasco-santos (as you probably know a load more about all of this)