# Setting up a PeerManager

The PeerManager is responsible for managing a set of peer connections and data associated with those peers.

# Adding a PeerManager

To add a PeerManager to your application, run:

  • Rust
  • Kotlin
  • Swift
use lightning::ln::peer_handler::{PeerManager};

let mut ephemeral_bytes = [0; 32];
rand::thread_rng().fill_bytes(&mut ephemeral_bytes);

let lightning_msg_handler = MessageHandler {
  chan_handler: channel_manager,
  route_handler: gossip_sync,
  onion_message_handler: onion_messenger,
  custom_message_handler: IgnoringMessageHandler {}

let peer_manager = PeerManager::new(
    cur_time.as_secs().try_into().map_err(|e| {
			log_error!(logger, "Failed to get current time: {}", e);

Implementation notes: if you did not initialize P2PGossipSync in the previous step, you can initialize your own struct (which can be a dummy struct) that implements RoutingMessageHandler

Dependencies: ChannelManager, RoutingMessageHandler, KeysManager, random bytes, Logger

References: Rust PeerManager docs (opens new window), Rust RoutingMessageHandler docs (opens new window), Java/Kotlin PeerManager bindings (opens new window), Java/Kotlin RoutingMessageHandler bindings (opens new window)