holepunchto / hyperswarm

A distributed networking stack for connecting peers.
https://docs.holepunch.to
MIT License
1.04k stars 84 forks source link

[Feature] lan discovery (broadcast/multicast?) #115

Open freddi301 opened 1 year ago

freddi301 commented 1 year ago

In alternative is there a way to connect directly to a peer by ip:port?

robbiemu commented 1 year ago

I believe this is the documentation site. this is provides a friendly wrapper around/is based on @hyperswarm/dht

"In the Hyperswarm DHT, peers are identified by a public key, not by an IP address. If you know someone's public key, you can connect to them regardless of where they're located". The underlying p2p mechanism is described in https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf.

I don't believe you can do that without replacing @hyperswarm/dht -- probably out of scope for this project.

What I am wondering is, how do the dht nodes traffic their information? I havent actually read that PDF yet :P -- but I dont expect that the server related aspect of it is covered there.

cjmont commented 9 months ago

Connecting to different peers with specific IPs in a P2P network using Hyperswarm is not the typical way to use the library, as Hyperswarm is designed to handle peer discovery and connection management for you, using a Distributed Hash Table (DHT) for peer discovery based on a specific "topic."

However, if you need to establish connections with specific IPs, you might need to use a direct connection solution using the net module in Node.js, as mentioned in previous answers, or look for a different P2P network library that allows direct connections to specific IPs and ports.

Example of Direct Connection with Node.js net:

Server (IP: 192.168.1.2, Port: 3000)

const net = require('net');

const server = net.createServer((socket) => {
  console.log('New peer connected');

  socket.on('data', (data) => {
    console.log('Data received:', data.toString());
  });

  socket.write('Hello from the server');
});

server.listen(3000, '192.168.1.2', () => {
  console.log('Server listening on 192.168.1.2:3000');
});

Client (Connecting to 192.168.1.2:3000)

const net = require('net');

const socket = net.createConnection(3000, '192.168.1.2', () => {
  console.log('Connected to the server');

  socket.on('data', (data) => {
    console.log('Data received:', data.toString());
  });

  socket.write('Hello from the client');
});

This example establishes a direct TCP connection between a client and a server using specific IP addresses and ports. If you need a full P2P solution with the ability to connect to specific peers by IP and port, you might need to look for a different library or build a custom solution that combines Hyperswarm for peer discovery and direct TCP connections for specific peers.

Keep in mind that direct connections can be problematic if peers are behind NATs or firewalls, and you might need to implement NAT traversal techniques to establish successful connections in such cases.

freddi301 commented 9 months ago

My scenario was developing a p2p application. What i had to do is to develop a seprata module for LAN connections. My quetion here is if hyperswarm could be shipped with LAN discovery incorporated to make the use of it more seamless.