Replies: 4 comments 4 replies
-
This is expected, mNDS only allows for discovery on the local network. See #3183 for a related discussion. |
Beta Was this translation helpful? Give feedback.
-
03-chat-gossip-kad-args.rs works on local network without mDNS |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. In Golang I've implemented an entire chat in ~100 lines. In Rust I am not even sure where to start (I have working chat with mDNS only). Here is my Golang code. I am looking for the If I am not mistaken
The majority of this code comes from official examples. It would be nice if Rust examples would be usable as a starting point for real-world apps. package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"sync"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
dutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
"github.com/fatih/color"
)
var (
topicNameFlag = flag.String("topicName", "chatroom", "topic name")
hostName = flag.String("host", "/ip4/0.0.0.0/tcp/0", "host ip")
)
func main() {
flag.Parse()
ctx := context.Background()
h, err := libp2p.New(libp2p.ListenAddrStrings(*hostName), libp2p.NATPortMap())
if err != nil {
panic(err)
}
go discover(ctx, h)
ps, err := pubsub.NewGossipSub(ctx, h)
if err != nil {
panic(err)
}
topic, err := ps.Join(*topicNameFlag)
if err != nil {
panic(err)
}
go writeMessage(ctx, topic)
sub, err := topic.Subscribe()
if err != nil {
panic(err)
}
readMessage(ctx, sub)
}
func discover(ctx context.Context, h host.Host) {
kademliaDHT, err := dht.New(ctx, h)
if err != nil {
panic(err)
}
if err = kademliaDHT.Bootstrap(ctx); err != nil {
panic(err)
}
var wg sync.WaitGroup
for _, peerAddr := range dht.DefaultBootstrapPeers {
peerinfo, _ := peer.AddrInfoFromP2pAddr(peerAddr)
wg.Add(1)
go func() {
defer wg.Done()
if err := h.Connect(ctx, *peerinfo); err != nil {
fmt.Println("Bootstrap warning:", err)
}
}()
}
wg.Wait()
routingDiscovery := drouting.NewRoutingDiscovery(kademliaDHT)
dutil.Advertise(ctx, routingDiscovery, *topicNameFlag)
anyConnected := false
for !anyConnected {
fmt.Println("Searching for peers...")
peerChan, err := routingDiscovery.FindPeers(ctx, *topicNameFlag)
if err != nil {
panic(err)
}
for peer := range peerChan {
if peer.ID == h.ID() {
continue
}
err := h.Connect(ctx, peer)
if err != nil {
fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
} else {
fmt.Printf("Connected to: %s\n", color.GreenString(peer.ID.String()))
anyConnected = true
}
}
}
fmt.Println("Peer discovery complete")
}
func writeMessage(ctx context.Context, topic *pubsub.Topic) {
reader := bufio.NewReader(os.Stdin)
for {
s, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
if err := topic.Publish(ctx, []byte(s)); err != nil {
fmt.Println("### Publish error:", err)
}
}
}
func readMessage(ctx context.Context, sub *pubsub.Subscription) {
for {
m, err := sub.Next(ctx)
if err != nil {
panic(err)
}
peerID := m.ReceivedFrom.String()
coloredPeerID := color.CyanString(peerID)
fmt.Printf("%s: %s\n", coloredPeerID, string(m.Message.Data))
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
The gossipsub-chat example works fine with mDNS, but not work over internet. Can anyone have idea to make it work?
Beta Was this translation helpful? Give feedback.
All reactions