PeerNet.Discovery.UDP (PeerNet v0.1.0)

Copy Markdown View Source

LAN UDP-broadcast discovery. Each instance:

  1. Opens a UDP socket on :listen_port (default 4040).
  2. Periodically (every :interval_ms, default 5_000) broadcasts a PeerNet.Discovery.UDP.Wire frame containing its pubkey + TCP listen port to 255.255.255.255:listen_port.
  3. Listens for the same shape from other instances. Each parsed announce becomes a {:peer_discovered, pubkey, address} message to the configured Registry.

When this works

  • Same LAN with broadcast permitted (typical home WiFi, dev networks).
  • Desktop, Nerves, and other "trusted local network" deployments.
  • The walkie-talkie demo on a laptop's local WiFi.

When this doesn't work

  • Across NAT boundaries (broadcast doesn't traverse routers).
  • On corporate / cellular networks that suppress broadcast traffic.
  • On iOS / Android without the appropriate platform permissions declared (NSLocalNetworkUsageDescription on iOS).

For mobile, the right layering is for the host app's NIF to provide a Discovery impl that wraps Bonjour / NSD — those use the same multicast protocol but go through OS-blessed APIs that don't trip permission prompts.

Self-suppression

We drop announces matching our own pubkey on the listen path — otherwise the broadcast would loop back to ourselves and noise up the Registry.

Cadence

Default cadence is 5s broadcast. Pair this with a Registry-side TTL of (say) 2 * interval_ms to detect peers leaving the network — the Registry treats peers it hasn't heard from in that window as stale. (TTL handling lives in the Registry, not here.)

Testability

The :transport option (default PeerNet.Discovery.UDP.Transport.GenUDP) lets tests inject a mock that doesn't actually open a socket, so unit tests can drive packets in/out without binding ports.

Summary

Functions

Returns a specification to start this module under a supervisor.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.