This is a POC, about implementing the libp2p
's go-libp2p
package for implementing a basic peer-to-peer publisher/subscriber (pubsub) system with using the go-libp2p-pubsub
package. Using the gossipsub
peer-to-peer pubsub implementations.
libp2p is a network framework that allows you to write decentralized peer-to-peer applications. It evolved out of IPFS
and became an independent project. There are certainly a ton of applications being built using libp2p
. IPFS
, Parity
, Ethereum
, Filecoin
, Polkadot
are some popular examples. libp2p
provides flexible solutions for essential peer-to-peer elements like transport, security, peer-routing, and content-discovery. It consists of a catalogue of modules from which p2p network developers can select and reuse just the protocols they need, while making it easy to upgrade and interoperate between applications. libp2p has implementations in Go
, JavaScript
, Rust
, Python
, and C++
. The specification of libp2p can be found in its specs repo.
The libp2p-pubsub has created a number of peer-to-peer pubsub implementations(e.g gossipsub, floodsub, fpisub) that have enabled real time peer-to-peer pubsub application development(read more about these different pubsub implementation from here). gossipsub
is named after the fact that peers gossip to each other about which messages they have seen and use this information to maintain a message delivery network.
In this post I have discussed about developing simple pubsub application with libp2p gossipsub. In the pubsub application, the publisher peer take messages from command line and push them to a topic called p2p-pubsub. Then the subscribers of that p2p-pubsub topic receive the data via peer-to-peer gossipsub. This is a simplified version of my pubsub system.
![Screenshot 2024-02-15 at 1 11 32 PM](https://private-user-images.githubusercontent.com/67535968/304988679-fe02f53f-d5c1-4326-ad36-a6b7afedfa98.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEyNTM0NzYsIm5iZiI6MTcyMTI1MzE3NiwicGF0aCI6Ii82NzUzNTk2OC8zMDQ5ODg2NzktZmUwMmY1M2YtZDVjMS00MzI2LWFkMzYtYTZiN2FmZWRmYTk4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE3VDIxNTI1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTAxZGE2OWI0OGJmNGVmMDE4MDk3NmViN2VhZGRhOGQ0MGIxYjA5NzFhNWQxMjkzNDE1NDg3MWEzMTUwYTRkYzYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.cDBxHfl6C-qwB2N6KejMZISonYxzcTWs-cN7TCnMYi4)
![Screenshot 2024-02-15 at 1 12 22 PM](https://private-user-images.githubusercontent.com/67535968/304988875-c1568a4f-dc72-403b-8eb7-a4133b25e0ba.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEyNTM0NzYsIm5iZiI6MTcyMTI1MzE3NiwicGF0aCI6Ii82NzUzNTk2OC8zMDQ5ODg4NzUtYzE1NjhhNGYtZGM3Mi00MDNiLThlYjctYTQxMzNiMjVlMGJhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE3VDIxNTI1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTBhMGU3MTJmN2NiNTgyNmY5NTFkZDk2MWU5NDFkODBjYjdmYTI5YzRiNGViMTFkZTQ4NzUzNTJmNDNlM2UwZjAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.9worXLKXdk2sGGMHFGnAqz71t1MXKcnuChV0LblPzyU)
![Screenshot 2024-02-15 at 1 12 42 PM](https://private-user-images.githubusercontent.com/67535968/304989058-9c3c295f-df38-44af-a6de-a3e855ff935e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEyNTM0NzYsIm5iZiI6MTcyMTI1MzE3NiwicGF0aCI6Ii82NzUzNTk2OC8zMDQ5ODkwNTgtOWMzYzI5NWYtZGYzOC00NGFmLWE2ZGUtYTNlODU1ZmY5MzVlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE3VDIxNTI1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTc3MGQ5YTRlZjBhMjY2YjUzMDRmYmU2NTNiNzg4NzM2ZmMyZDRkYjgyNmFlNTJjZWRiNTE2ZDYxOTllMDFkZDEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.YsZZP89gn9crdjdhtwZHlcCpLrpkzFOFaH3eDzXPWEM)
Follow the below steps to run your local pubsub system:
- Clone the repository by running this command:
git clone https://github.com/amancooks08/p2p-poc.git
. - Enter the directory by running the command
cd p2p-poc
. - Run the
go mod tidy
to make sure you have all the dependencies installed. - Open a Terminal and start your subscriber, by running the command:
go run subscriber/subscriber.go
. - Open another terminal and start your publisher, by running the command:
go run publisher/publisher.go
. - Send a message and check in the subscriber terminal to see that it has been received.