Zack Scholl

zack.scholl@gmail.com

Peer discovery

 / #software #portfolio 

Go library for cross-platform local peer discovery.

Pure-go library for cross-platform thread-safe local peer discovery using UDP multicast. I needed to use peer discovery for croc and everything I tried had problems, so I made another one.

Install

Make sure you have Go 1.5+.

go get -u github.com/schollz/peerdiscovery

Usage

The following is a code to find the first peer on the local network and print it out.

1discoveries, _ := peerdiscovery.Discover(peerdiscovery.Settings{Limit: 1})
2for _, d := range discoveries {
3    fmt.Printf("discovered '%s'\n", d.Address)
4}

Here’s the output when running on two computers. (Run these gifs in sync by hitting Ctl + F5).

Computer 1:

computer 1

Computer 2:

computer 1

For more examples, see the scanning examples (ipv4 and ipv6) or the docs.

Testing

To test the peer discovery with just one host, one can launch multiple containers. The provided Dockerfile will run the example code. Please make sure to enable Docker’s IPv6 support if you are using IPv6 for peer discovery.

1# Build the container, named peertest
2$ docker build -t peertest .
3
4# Execute the following command in multiple terminals
5$ docker run -t --rm peertest
6Scanning for 10 seconds to find LAN peers
7 100% |████████████████████████████████████████|  [9s:0s]Found 1 other computers
80) '172.17.0.2' with payload 'zqrecHipCO'

Contributing

Pull requests are welcome. Feel free to…

Thanks

Thanks @geistesk for adding IPv6 support and a Notify func, and helping maintain! Thanks @Kunde21 for providing a bug fix and massively refactoring the code in a much better way. Thanks @robpre for finding and fixing bugs. Thanks @shvydky for adding dynamic payloads.