register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#Notifee) with service so that we get notified about peer discovery
register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#Notifee) with service so that we get notified about peer discovery
```go
```go
n := &discoveryNotifee{}
ser.Start()
ser.RegisterNotifee(n)
```
```
4. **Open streams to peers found.**
4. **Open streams to peers found.**
Finally we open stream to the peers we found, as we find them
Finally we open stream to the peers we found, as we find them
```go
```go
peer := <-peerChan//willblockuntilwediscoverapeer
peer := <-peerChan//willblockuntilwediscoverapeer
// this is used to avoid call `NewStream` from both side
if peer.ID > host.ID() {
// if other end peer id greater than us, don't connect to it, just wait for it to connect us
fmt.Println("Found peer:", peer, " id is greater than us, wait for it to connect to us")
continue
}
fmt.Println("Found peer:", peer, ", connecting")
fmt.Println("Found peer:", peer, ", connecting")
if err := host.Connect(ctx, peer); err != nil {
if err := host.Connect(ctx, peer); err != nil {
fmt.Println("Connection failed:", err)
fmt.Println("Connection failed:", err)
continue
}
}
// open a stream, this stream will be handled by handleStream other end
// open a stream, this stream will be handled by handleStream other end