Connect many peers
Last updated
Last updated
In the former example, two peers connected directly using the first peer's public key. Hyperswarm helps to discover peers swarming a common topic, and connect to as many of them as possible. This will become clearer in the Hypercore example, but it's the best way to distribute peer-to-peer data structures.
The module provides a higher-level interface over the underlying DHT, abstracting away the mechanics of establishing and maintaining connections. Instead, 'join' topics, and the swarm discovers peers automatically. It also handles reconnections in the event of failures.
In the , we needed to explicitly indicate which peer was the server and which was the client. By using Hyperswarm, we create two peers, have them join a common topic, and let the swarm deal with connections.
This How-to consists of a single application, peer-app
.
Create the peer-app
project with the following commands:
Alter the peer-app/index.js file to the following:
In one terminal, open peer-app
with pear run --dev .
This will display the topic. Copy/paste that topic into as many additional terminals as desired with pear run --dev . <SUPPLY TOPIC HERE>
(assuming that the current working directory of each terminal is the peer-app
folder). Each peer will log information about the other connected peers.
Start typing into any terminal, and it will be broadcast to all connected peers.