|
| 1 | +# Connecting to a peer |
| 2 | + |
| 3 | +In this section you'll learn how to join the lightning network. |
| 4 | + |
| 5 | +Firstly we need to have the ability to do high performance I/O operations. LDK provides default implementations for all of your networking needs. If you are using Rust, you can use our simple socket handling library `lightning_net_tokio`. In Java you can use the `NioPeerHandler` which uses Java's NIO I/O interface. |
| 6 | + |
| 7 | +Connections to other peers are established with `PeerManager`. You'll need to know the pubkey and address of another node that you want as a peer. Once the connection is established and the handshake is complete, `PeerManager` will show the peer's pubkey in its list of peers. |
| 8 | + |
| 9 | +<CodeSwitcher :languages="{rust:'Rust', java:'Java', kotlin:'Kotlin'}"> |
| 10 | + <template v-slot:rust> |
| 11 | + |
| 12 | +```rust |
| 13 | +match lightning_net_tokio::connect_outbound(Arc::clone(&peer_manager), pubkey, address).await { |
| 14 | + Some(connection_closed_future) => { |
| 15 | + let mut connection_closed_future = Box::pin(connection_closed_future); |
| 16 | + loop { |
| 17 | + // Make sure the connection is still established. |
| 18 | + match futures::poll!(&mut connection_closed_future) { |
| 19 | + std::task::Poll::Ready(_) => { |
| 20 | + panic!("ERROR: Peer disconnected before handshake completed"); |
| 21 | + } |
| 22 | + std::task::Poll::Pending => {} |
| 23 | + } |
| 24 | + |
| 25 | + // Wait for the handshake to complete. |
| 26 | + match peer_manager.get_peer_node_ids().iter().find(|id| **id == pubkey) { |
| 27 | + Some(_) => break, |
| 28 | + None => tokio::time::sleep(std::time::Duration::from_millis(10)).await, |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + None => panic!("ERROR: Failed to connect to peer"), |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | + </template> |
| 37 | + |
| 38 | + <template v-slot:java> |
| 39 | + |
| 40 | +```java |
| 41 | +try { |
| 42 | + // Connect and wait for the handshake to complete. |
| 43 | + SocketAddress address = new InetSocketAddress(host, port); |
| 44 | + nio_peer_handler.connect(pubkey, address); |
| 45 | + |
| 46 | + // The peer's pubkey will be present in the list of peer ids. |
| 47 | + final PeerManager peer_manager = channel_manager_constructor.peer_manager; |
| 48 | + byte[][] peer_node_ids = peer_manager.get_peer_node_ids(); |
| 49 | +} catch (java.io.IOException e) { |
| 50 | + // Handle failure to successfully connect to a peer. |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | + </template> |
| 55 | + |
| 56 | + <template v-slot:kotlin> |
| 57 | + |
| 58 | +```kotlin |
| 59 | +try { |
| 60 | + // Connect and wait for the handshake to complete. |
| 61 | + val address: SocketAddress = InetSocketAddress(hostname, port) |
| 62 | + nioPeerHandler.connect(pubkeyHex.toByteArray(), address, 5555) |
| 63 | + |
| 64 | + // The peer's pubkey will be present in the list of peer ids. |
| 65 | + val peerManager: PeerManager = channelManagerConstructor.peer_manager |
| 66 | + val peerNodeIds = peerManager._peer_node_ids |
| 67 | + |
| 68 | + } catch (e: IOException) { |
| 69 | + // Handle failure to successfully connect to a peer. |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | + </template> |
| 74 | +</CodeSwitcher> |
| 75 | + |
| 76 | +**Dependencies:** `PeerManager` |
| 77 | + |
| 78 | +**References:** [Rust `lightning-net-tokio` docs](https://docs.rs/lightning-net-tokio/*/lightning_net_tokio/), [Rust `PeerManager` docs](https://docs.rs/lightning/*/lightning/ln/peer_handler/struct.PeerManager.html), [Java `NioPeerHandler` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/batteries/NioPeerHandler.java), |
| 79 | +[Java `PeerManager` docs](https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/structs/PeerManager.java), |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
0 commit comments