Connect two peers
HyperDHT
helps clients connect to a server peer with a known public key. HyperDHT uses a series of holepunching techniques to establish direct connections between the peers, even if they're located on home networks with tricky NATs.
In the HyperDHT, peers are identified by a public key, not by an IP address. The public key is looked up in a decentralized hash table, which maps the key to an IP address and port. This means users can connect to each other irrespective of their location, even if they move between different networks.
HyperDHT's holepunching will fail if both the client peer and the server peer are on randomizing NATs, in which case the connection must be relayed through a third peer. HyperDHT does not do any relaying by default.
For example, Keet implements its relaying system wherein other call participants can serve as relays -- the more participants in the call, the stronger overall connectivity becomes.
Use the HyperDHT to create a basic CLI chat app where a client peer connects to a server peer by public key.
This example consists of two applications: client-app
and server-app
.
The server-app
will create a key pair and then start a server that will listen on the generated key pair. The public key is logged into the console. Copy it for instantiating the client.
Create the server-app
project with the following commands:
Alter server-app/index.js
to the following:
Open the server-app
with pear run --dev .
.
Create the client-app
project with the following commands:
Alter client-app/index.js
to the following:
Pass the key to the client:
The client-app
will spin up a client, and the public key copied earlier must be supplied as a command line argument for connecting to the server. The client process will log got connection
into the console when it connects to the server.
Once it's connected, try typing in both terminals.
Last updated