|
| 1 | +Rendezvous Protocol Demo |
| 2 | +======================== |
| 3 | + |
| 4 | +This example demonstrates the **rendezvous protocol** for peer discovery in libp2p networks. The rendezvous protocol allows peers to register under namespaces and discover other peers within the same namespace, facilitating peer-to-peer communication without requiring direct connections. |
| 5 | + |
| 6 | +Overview |
| 7 | +-------- |
| 8 | + |
| 9 | +The rendezvous protocol consists of two main components: |
| 10 | + |
| 11 | +1. **Rendezvous Server**: Acts as a registry where peers can register and discover each other |
| 12 | +2. **Rendezvous Client**: Registers with the server and discovers other peers in the same namespace |
| 13 | + |
| 14 | +Key Features |
| 15 | +------------ |
| 16 | + |
| 17 | +- **Namespace-based Discovery**: Peers register under specific namespaces for organized discovery |
| 18 | +- **Automatic Refresh**: Optional background refresh to maintain registrations and discovery cache |
| 19 | +- **TTL Management**: Time-based expiration of registrations to prevent stale entries |
| 20 | +- **Peer Advertisement**: Peers can advertise their presence and availability |
| 21 | +- **Scalable Discovery**: Efficient peer discovery without flooding the network |
| 22 | + |
| 23 | +Quick Start |
| 24 | +----------- |
| 25 | + |
| 26 | +1. **Install py-libp2p:** |
| 27 | + |
| 28 | +.. code-block:: console |
| 29 | +
|
| 30 | + $ python -m pip install libp2p |
| 31 | +
|
| 32 | +2. **Start a Rendezvous Server:** |
| 33 | + |
| 34 | +.. code-block:: console |
| 35 | +
|
| 36 | + $ python rendezvous.py --mode server |
| 37 | + 2025-09-21 14:05:47,378 [INFO] [libp2p.discovery.rendezvous.service] Rendezvous service started |
| 38 | + 2025-09-21 14:05:47,378 [INFO] [rendezvous_example] Rendezvous server started with peer ID: Qmey5ZN9WjvtjzYrDfv3NYUY61tusn1qyHAWpuT5vaWUUR |
| 39 | + 2025-09-21 14:05:47,378 [INFO] [rendezvous_example] Listening on: /ip4/0.0.0.0/tcp/51302/p2p/Qmey5ZN9WjvtjzYrDfv3NYUY61tusn1qyHAWpuT5vaWUUR |
| 40 | + 2025-09-21 14:05:47,378 [INFO] [rendezvous_example] To connect a client, use: |
| 41 | + 2025-09-21 14:05:47,378 [INFO] [rendezvous_example] python rendezvous.py --mode client --address /ip4/0.0.0.0/tcp/51302/p2p/Qmey5ZN9WjvtjzYrDfv3NYUY61tusn1qyHAWpuT5vaWUUR |
| 42 | + 2025-09-21 14:05:47,378 [INFO] [rendezvous_example] Press Ctrl+C to stop... |
| 43 | +
|
| 44 | +3. **Connect Clients (in separate terminals):** |
| 45 | + |
| 46 | +.. code-block:: console |
| 47 | +
|
| 48 | + $ python rendezvous.py --mode client --address /ip4/0.0.0.0/tcp/51302/p2p/Qmey5ZN9WjvtjzYrDfv3NYUY61tusn1qyHAWpuT5vaWUUR |
| 49 | + 2025-09-21 14:07:07,641 [INFO] [rendezvous_example] Connected to rendezvous server: Qmey5ZN9WjvtjzYrDfv3NYUY61tusn1qyHAWpuT5vaWUUR |
| 50 | + 2025-09-21 14:07:07,641 [INFO] [rendezvous_example] Enable refresh: True |
| 51 | + 2025-09-21 14:07:07,641 [INFO] [rendezvous_example] 🔄 Refresh mode enabled - discovery service running in background |
| 52 | + 2025-09-21 14:07:07,642 [INFO] [rendezvous_example] Client started with peer ID: QmWyrP7nwTaDDaM4CayBybs6aATNM4CYmbmXDU6oPADN7Y |
| 53 | + 2025-09-21 14:07:07,644 [INFO] [rendezvous_example] Registering in namespace 'rendezvous'... |
| 54 | + 2025-09-21 14:07:07,645 [INFO] [rendezvous_example] ✓ Registered with TTL 7200s |
| 55 | + 2025-09-21 14:07:08,652 [INFO] [rendezvous_example] Discovering peers in namespace 'rendezvous'... |
| 56 | + 2025-09-21 14:07:08,653 [INFO] [rendezvous_example] Found self: QmWyrP7nwTaDDaM4CayBybs6aATNM4CYmbmXDU6oPADN7Y |
| 57 | + 2025-09-21 14:07:08,653 [INFO] [rendezvous_example] Total peers found: 1 |
| 58 | + 2025-09-21 14:07:08,653 [INFO] [rendezvous_example] No other peers found (only self) |
| 59 | +
|
| 60 | +Usage Examples |
| 61 | +-------------- |
| 62 | + |
| 63 | +Basic Server |
| 64 | +~~~~~~~~~~~~ |
| 65 | + |
| 66 | +Start a rendezvous server on a specific port: |
| 67 | + |
| 68 | +.. code-block:: console |
| 69 | +
|
| 70 | + $ python rendezvous.py --mode server --port 8080 |
| 71 | +
|
| 72 | +Client with Custom Namespace |
| 73 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 | + |
| 75 | +Register and discover peers in a custom namespace: |
| 76 | + |
| 77 | +.. code-block:: console |
| 78 | +
|
| 79 | + $ python rendezvous.py --mode client --address <server_multiaddr> --namespace "my-app" |
| 80 | +
|
| 81 | +Client without Refresh |
| 82 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 83 | + |
| 84 | +Run a client without automatic refresh (single-shot mode): |
| 85 | + |
| 86 | +.. code-block:: console |
| 87 | +
|
| 88 | + $ python rendezvous.py --mode client --address <server_multiaddr> --refresh False |
| 89 | +
|
| 90 | +Verbose Logging |
| 91 | +~~~~~~~~~~~~~~~ |
| 92 | + |
| 93 | +Enable debug logging for detailed information: |
| 94 | + |
| 95 | +.. code-block:: console |
| 96 | +
|
| 97 | + $ python rendezvous.py --mode server --verbose |
| 98 | +
|
| 99 | +Command Line Options |
| 100 | +-------------------- |
| 101 | + |
| 102 | +.. code-block:: text |
| 103 | +
|
| 104 | + usage: rendezvous.py [-h] [--mode {server,client}] [--address [ADDRESS]] |
| 105 | + [-p PORT] [-n NAMESPACE] [-v] [-r] |
| 106 | +
|
| 107 | + optional arguments: |
| 108 | + -h, --help show this help message and exit |
| 109 | + --mode {server,client} |
| 110 | + Run as server or client |
| 111 | + --address [ADDRESS] Server multiaddr (required for client mode) |
| 112 | + -p PORT, --port PORT Port for server to listen on (default: random) |
| 113 | + -n NAMESPACE, --namespace NAMESPACE |
| 114 | + Namespace to register/discover in (default: rendezvous) |
| 115 | + -v, --verbose Enable verbose logging |
| 116 | + -r, --refresh Enable automatic refresh for registration and discovery cache |
| 117 | +
|
| 118 | +Protocol Flow |
| 119 | +------------- |
| 120 | + |
| 121 | +1. **Server Setup**: The rendezvous server starts and listens for incoming connections |
| 122 | +2. **Client Connection**: Clients connect to the server using its multiaddr |
| 123 | +3. **Registration**: Clients register themselves under a namespace with a TTL |
| 124 | +4. **Discovery**: Clients query the server for other peers in the same namespace |
| 125 | +5. **Refresh**: (Optional) Clients automatically refresh their registration before TTL expires |
| 126 | +6. **Unregistration**: Clients cleanly unregister when shutting down |
| 127 | + |
| 128 | +Key Components |
| 129 | +-------------- |
| 130 | + |
| 131 | +RendezvousService |
| 132 | +~~~~~~~~~~~~~~~~~ |
| 133 | + |
| 134 | +The server-side component that: |
| 135 | + |
| 136 | +- Manages peer registrations by namespace |
| 137 | +- Handles registration, unregistration, and discovery requests |
| 138 | +- Automatically cleans up expired registrations |
| 139 | +- Provides namespace statistics |
| 140 | + |
| 141 | +RendezvousDiscovery |
| 142 | +~~~~~~~~~~~~~~~~~~~ |
| 143 | + |
| 144 | +The client-side component that: |
| 145 | + |
| 146 | +- Registers the local peer under namespaces |
| 147 | +- Discovers other peers in namespaces |
| 148 | +- Optionally runs background refresh tasks |
| 149 | +- Manages registration TTL and cache refresh |
| 150 | + |
| 151 | +Configuration |
| 152 | +------------- |
| 153 | + |
| 154 | +Default values can be customized: |
| 155 | + |
| 156 | +.. code-block:: python |
| 157 | +
|
| 158 | + from libp2p.discovery.rendezvous import config |
| 159 | +
|
| 160 | + # Default namespace for registrations |
| 161 | + config.DEFAULT_NAMESPACE = "rendezvous" |
| 162 | +
|
| 163 | + # Default TTL for registrations (2 hours) |
| 164 | + config.DEFAULT_TTL = 2 * 3600 |
| 165 | +
|
| 166 | + # Maximum number of registrations per namespace |
| 167 | + config.MAX_REGISTRATIONS = 1000 |
| 168 | +
|
| 169 | + # Maximum TTL allowed |
| 170 | + config.MAX_TTL = 24 * 3600 # 24 hours |
| 171 | +
|
| 172 | +Refresh Mode |
| 173 | +------------ |
| 174 | + |
| 175 | +When refresh mode is enabled (default), the client: |
| 176 | + |
| 177 | +- Automatically re-registers before the TTL expires (at 80% of TTL) |
| 178 | +- Refreshes the discovery cache periodically |
| 179 | +- Runs a background service using trio's structured concurrency |
| 180 | +- Maintains long-term presence in the network |
| 181 | + |
| 182 | +This is ideal for long-running applications that need continuous peer discovery. |
| 183 | + |
| 184 | +Use Cases |
| 185 | +--------- |
| 186 | + |
| 187 | +- **Distributed Applications**: Services that need to find each other dynamically |
| 188 | +- **Gaming**: Players discovering game sessions or lobbies |
| 189 | +- **Content Sharing**: Nodes advertising available content or services |
| 190 | +- **Mesh Networks**: Peers discovering neighbors in decentralized networks |
| 191 | +- **Service Discovery**: Microservices finding each other in P2P architectures |
| 192 | + |
| 193 | +Error Handling |
| 194 | +-------------- |
| 195 | + |
| 196 | +The implementation includes robust error handling: |
| 197 | + |
| 198 | +- Connection failures to rendezvous servers |
| 199 | +- Registration timeouts and failures |
| 200 | +- Discovery query errors |
| 201 | +- Background refresh task failures |
| 202 | +- Network connectivity issues |
| 203 | + |
| 204 | +Best Practices |
| 205 | +-------------- |
| 206 | + |
| 207 | +1. **Use descriptive namespaces** to organize different types of peers |
| 208 | +2. **Enable refresh mode** for long-running applications |
| 209 | +3. **Set appropriate TTL values** based on your application's needs |
| 210 | +4. **Handle connection failures** gracefully in production code |
| 211 | +5. **Monitor namespace statistics** on the server for debugging |
| 212 | +6. **Use verbose logging** during development and testing |
| 213 | + |
| 214 | +Source Code |
| 215 | +----------- |
| 216 | + |
| 217 | +.. literalinclude:: ../examples/rendezvous/rendezvous.py |
| 218 | + :language: python |
| 219 | + :linenos: |
| 220 | + |
| 221 | +API Reference |
| 222 | +------------- |
| 223 | + |
| 224 | +For detailed API documentation, see: |
| 225 | + |
| 226 | +- :doc:`libp2p.discovery` - Discovery protocol interfaces |
| 227 | +- :doc:`libp2p.discovery.rendezvous` - Rendezvous implementation details |
0 commit comments