You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## 3. Where to find other Peer Discovery Mechanisms
159
+
## 3. Pubsub based Peer Discovery
160
+
161
+
For this example, we need [`libp2p-pubsub-peer-discovery`](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/), go ahead and `npm install` it. You also need to spin up a set of [`libp2p-relay-servers`](https://github.com/libp2p/js-libp2p-relay-server). These servers act as relay servers and a peer discovery source.
162
+
163
+
In the context of this example, we will create and run the `libp2p-relay-server` in the same code snippet. You can find the complete solution at [3.js](./3.js).
We will use the `libp2p-relay-server` as bootstrap nodes for the libp2p nodes, so that they establish a connection with the relay after starting. As a result, after they establish a connection with the relay, the pubsub discovery will kick in an the relay will advertise them.
207
+
208
+
```js
209
+
constrelay=awaitcreateRelayServer({
210
+
listenAddresses: ['/ip4/0.0.0.0/tcp/0']
211
+
})
212
+
console.log(`libp2p relay starting with id: ${relay.peerId.toB58String()}`)
Taking into account the output, after the relay and both libp2p nodes start, both libp2p nodes will discover the bootstrap node (relay) and connect with it. After establishing a connection with the relay, they will discover each other.
249
+
250
+
This is really useful when running libp2p in constrained environments like a browser. You can run a set of `libp2p-relay-server` nodes that will be responsible for both relaying websocket connections between browser nodes and for discovering other browser peers.
251
+
252
+
## 4. Where to find other Peer Discovery Mechanisms
160
253
161
254
There are plenty more Peer Discovery Mechanisms out there, you can:
0 commit comments