Skip to content

A Rust-based example of creating a peer-to-peer network using WebSockets for real-time communication between nodes.

Notifications You must be signed in to change notification settings

mohyfahim/p2pws-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

P2P WebSocket Network Example

This project demonstrates how to create a peer-to-peer (P2P) network over a WebSocket infrastructure using Rust and the Tokio async runtime. The example code allows nodes to connect to each other, forming a distributed network where messages can be exchanged between peers.

Features

  • Establish a P2P network using WebSockets.
  • Support for both client and server WebSocket connections.
  • Broadcast messages to all connected peers.
  • Graceful shutdown on receiving Ctrl+C.

Dependencies

This project uses the following Rust crates:

  • tokio: For asynchronous programming and handling I/O operations.
  • tokio-tungstenite: For WebSocket client and server implementation.
  • futures: For handling asynchronous streams and sinks.
  • clap: For parsing command-line arguments.
  • log: For logging information and errors.
  • env_logger: For configuring the log output.

Running

Ensure you have Rust and Cargo installed on your system. Clone the repository and navigate to the project directory:

$ git clone https://github.com/mohyfahim/p2pws-rs.git
$ cd p2pws-rs
$ cargo build

Then in separate sessions, run the program in orders with the following arguments:

$ RUST_LOG=debug cargo run -- --bind localhost:8080
$ RUST_LOG=debug cargo run -- --peers ws://localhost:8080 --bind localhost:8085
$ RUST_LOG=debug cargo run -- --peers ws://localhost:8080,ws://localhost:8085 --bind localhost:8086

Then you can see the message, broadcasting from each peer to all others. all-to-all broadcast!

About

A Rust-based example of creating a peer-to-peer network using WebSockets for real-time communication between nodes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages