Peerage v1.0.2 Peerage.Via.Udp

Use UDP multicast to find other nodes.

config :peerage, via: Peerage.Via.Udp, serves: true
  ip: {0,0,0,0},
  port: 45_900,
  multicast_addr: {230,1,1,1}

Will both broadcast and receive node names via UDP on port 45900, and keep track of ones it’s seen in process state. It’s a GenServer, so we let Peerage know it needs to be run and supervised with serves: true.

Peerage.Server periodically calls poll(), which is a client function for the GenServer’s handle_call(:poll, _, state), which returns the seen node names from state.

Only one node can bind the socket, but you can test multiple nodes using docker, like this.

For more info on UDP in Elixir, see this scaleSmall post on multicast UDP in Elixir from 2015, especially the explation of gen_udp’s active: N mode.

Summary

Functions

Server function: returns list of node names we’ve seen

Handle UDP packet. If it’s a node name broadcast, adds to state.seen

Client function: Peerage.Provider callback. Calls this GenServer

Functions

handle_call(msg, arg2, state)

Server function: returns list of node names we’ve seen.

handle_info(msg, state)

Handle UDP packet. If it’s a node name broadcast, adds to state.seen.

poll()

Client function: Peerage.Provider callback. Calls this GenServer.

start_link()
terminate(, , map)