MoonSHRD / p2chat

local communication core
5 stars 2 forks source link

Explain how p2p module works in this app? #80

Open JackBekket opened 4 days ago

JackBekket commented 4 days ago

Explain how p2p module works in this app?

githelperbot[bot] commented 4 days ago

The p2p module in this application is responsible for handling peer discovery and communication using the libp2p library. It leverages the libp2p-pubsub package for peer discovery and message exchange.

Here's a breakdown of how the p2p module works:

  1. Peer Discovery: The application uses mDNS (Multicast DNS) for peer discovery. It initializes an MDNS service with a rendezvous string (service tag) to discover peers with the same service tag.

  2. PubSub: The application uses libp2p-pubsub to create a PubSub instance with a specified protocol ID and message signing options. This allows peers to subscribe to topics and exchange messages.

  3. Peer Connection: When a new peer is discovered, the application connects to it and adds its address to the local peerstore.

  4. Message Handling: The application subscribes to the rendezvous topic and handles incoming messages from other peers. It prints incoming messages to the console with a green color.

  5. Main Loop: The application continuously listens for incoming messages and new peers. It handles incoming messages and connects to new peers.

  6. Network Topics Request: The application can request a list of all active topics from the PubSub instance to get an overview of the network's topics.

In summary, the p2p module in this application uses libp2p-pubsub and mDNS to enable peer discovery, connection, and communication. It allows peers to subscribe to topics, exchange messages, and maintain a list of connected peers.