You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This proposal outlines how we can introduce Multicast DNS (mDNS)-based peer discovery into py-libp2p, aligned with the behavior of go-libp2p and js-libp2p.
Module Structure & Responsibilities
We propose creating a dedicated module, libp2p.discovery.mdns, with the following internal components:
libp2p/
└── discovery/
└── mdns/
├── broadcaster.py # Registers local peer via mDNS
├── listener.py # Listens for mDNS broadcasts from other peers
└── __init__.py
Each component would manage distinct responsibilities:
Broadcaster
Advertises the local peer's presence on the network using the _p2p._udp.local service name.
Uses a TXT record to encode the local peer ID:
Example: id=QmPeerID123...
Listener
Listens on 224.0.0.251:5353 for mDNS responses.
Parses valid mDNS responses to extract PeerInfo for peers discovered on the same LAN.
Emits these to the libp2p Swarm/Host layer for dialing.
Integration Layer
Provides an API surface for enabling/disabling mDNS.
Hooks into Swarm or Host to trigger connection attempts when peers are discovered.
🛠️ How it Works (Expected Flow)
On startup, if mdns is enabled:
mdns_broadcaster advertises the peer using Zeroconf (or equivalent).
mdns_listener listens for peer advertisements.
When a peer is found:
A PeerInfo object is emitted.
If supported, Swarm auto-dials the discovered peer or adds it to its peerstore.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
This proposal outlines how we can introduce Multicast DNS (mDNS)-based peer discovery into
py-libp2p
, aligned with the behavior ofgo-libp2p
andjs-libp2p
.Module Structure & Responsibilities
We propose creating a dedicated module,
libp2p.discovery.mdns
, with the following internal components:Each component would manage distinct responsibilities:
Broadcaster
_p2p._udp.local
service name.Example:
id=QmPeerID123...
Listener
224.0.0.251:5353
for mDNS responses.PeerInfo
for peers discovered on the same LAN.Integration Layer
Swarm
orHost
to trigger connection attempts when peers are discovered.🛠️ How it Works (Expected Flow)
On startup, if
mdns
is enabled:mdns_broadcaster
advertises the peer using Zeroconf (or equivalent).mdns_listener
listens for peer advertisements.When a peer is found:
PeerInfo
object is emitted.Swarm
auto-dials the discovered peer or adds it to its peerstore.Dependency Suggestion:
[zeroconf](https://github.com/jstasiak/python-zeroconf)
Python LibraryWe suggest using
zeroconf
, a well-supported Python library with support for:The library is used in projects like Home Assistant, which shows it handles real-world, high-performance discovery.
Configuration Options
Add a new config object or flags to enable/disable mDNS:
Advanced options could include:
Testing Plan
Unit Tests
Integration Tests
We can leverage
pytest
fixtures to spin up hosts in subprocesses on local ports.Example Usage (Preview)
🔍 Open Questions for Discussion
Where should peer discovery events be surfaced?
Host
expose a listener pattern or use async callbacks?How do we ensure safety and scoping?
Do we want to align broadcast intervals with go/js defaults?
Should we emit events even if the peer is already known (i.e., for heartbeats)?
Why This Is a Good Fit for
py-libp2p
Beta Was this translation helpful? Give feedback.
All reactions