diff options
| author | elioat <elioat@tilde.institute> | 2025-11-15 21:33:54 -0500 |
|---|---|---|
| committer | elioat <elioat@tilde.institute> | 2025-11-15 21:33:54 -0500 |
| commit | 83a3ec1d9681cd9f15214d50339a48c906c72298 (patch) | |
| tree | 91068a3682c0d103f10d7bf7793104d5b0d1e419 | |
| parent | 951e643a88bd0e4afa2cf03351400ca1ebc7d7d6 (diff) | |
| download | tour-83a3ec1d9681cd9f15214d50339a48c906c72298.tar.gz | |
Update README: accurate, concise, reflects current implementation
- Removed timestamp mentions (feature was removed)
- Simplified and consolidated duplicate examples
- Updated message flow explanation with is_local logic
- Condensed extending section
- Removed repetitive architecture references
- Focused on essential information only
README is now terse and matches v1.0.0 behavior exactly.
| -rw-r--r-- | gleam/gossip/README.md | 211 |
1 files changed, 54 insertions, 157 deletions
diff --git a/gleam/gossip/README.md b/gleam/gossip/README.md index 2b45597..742bee8 100644 --- a/gleam/gossip/README.md +++ b/gleam/gossip/README.md @@ -1,15 +1,15 @@ -# Gossip: Decentralized P2P Chat +# Gossip: P2P Chat -A peer-to-peer chat system where multiple people can connect and chat without any central server. Built with Gleam (compile-to-Erlang) and the BEAM VM. +A decentralized peer-to-peer chat system. Multiple peers connect via Erlang's distributed protocol with no central server. Built with Gleam and the BEAM VM. -## What is Gossip? +## Features -Gossip is a decentralized messaging application that: -- **Requires no central server** - every peer is independent -- **Works across networks** - same machine, local network, or internet -- **Prevents message loops** - sophisticated origin tracking prevents infinite echoes -- **Type-safe** - Gleam's compiler catches bugs before runtime -- **Battle-tested** - uses Erlang's proven distributed systems technology +- **No central server** - every peer is independent and equal +- **Works anywhere** - same machine, local network, or across the internet +- **Loop-free messaging** - origin tracking prevents infinite message echoes +- **Eventually consistent** - all peers converge to the same state via gossip +- **Type-safe** - Gleam's compiler prevents entire categories of bugs +- **Built on proven tech** - Erlang has 30+ years of production-grade distributed systems ## Quick Start @@ -39,31 +39,17 @@ gossip alice gossip bob alice@127.0.0.1 ``` -### Try It Out +### Example: Same Machine (Localhost) -**Same Machine (Localhost):** ```bash # Terminal 1 ./bin/gossip alice -# Terminal 2 +# Terminal 2 ./bin/gossip bob alice@127.0.0.1 - -# Now type messages in either terminal - they appear with timestamps ``` -**Different Machines:** -```bash -# Machine 1 (bootstrap with --network flag) -./bin/gossip alice --network - -# Machine 2 (connect using hostname or IP) -./bin/gossip bob alice@machine1.local -# or -./bin/gossip bob alice@192.168.1.10 -``` - -Messages display as: `HH:MM:SS [sender] message`. Each message appears once per peer (no duplicates). Press `Ctrl+D` to quit. +Type messages in either terminal. They appear as `[sender] message`. Each message displays exactly once (no duplicates). Press `Ctrl+D` to quit. ## How It Works @@ -83,18 +69,18 @@ Peer ### Message Flow -1. **User types message** → sent to local registry -2. **Local registry**: - - Displays message immediately to user - - Forwards to all connected peers -3. **Remote registries**: - - Receive forwarded message +1. **User types** → input_loop reads message +2. **Local registry** receives message with `is_local=True` + - Does NOT display (terminal already echoed the input) + - Marks message as seen (prevents re-display if bounced back) + - Forwards to all connected peers with `is_local=False` +3. **Remote registries** receive with `is_local=False` - Display to their user - - Forward to their peers (except sender) -4. **Loop prevention**: Each message tracks its origin and last sender - - Origin ID: who created the message (never changes) - - Last sender ID: who forwarded it to us (changes each hop) - - When forwarding: update last sender ID to prevent bouncing back + - Forward to their peers (except sender, tracked via `last_sender_id`) +4. **Loop prevention**: Two-part tracking prevents infinite echoes + - `origin_id`: Who created the message (immutable) + - `last_sender_id`: Who just forwarded it to us (updated each hop) + - When forwarding: skip the sender to prevent bouncing back ### Example: Message Flow with 3 Peers @@ -123,81 +109,25 @@ Peers communicate via **Erlang's distributed protocol**, which uses: This is why `./bin/gossip alice@192.168.1.10` works - Erlang handles all the networking transparently. -## Usage Examples - -### Same Machine (Localhost) - Two Peers - -```bash -# Terminal 1 -./bin/gossip alice - -# Terminal 2 -./bin/gossip bob alice@127.0.0.1 -``` - -### Same Machine (Localhost) - Three+ Peers - -```bash -# Terminal 1 -./bin/gossip alice - -# Terminal 2 -./bin/gossip bob alice@127.0.0.1 - -# Terminal 3 -./bin/gossip carol alice@127.0.0.1 -``` - -### Different Machines on Local Network - -**Machine 1 (host: machine1.local, IP: 192.168.1.10):** -```bash -./bin/gossip alice --network -``` - -**Machine 2:** -```bash -# Using hostname -./bin/gossip bob alice@machine1.local - -# OR using IP -./bin/gossip bob alice@192.168.1.10 -``` - -### Internet (Different Networks) - -For peers on different networks, ensure: -1. Bootstrap peer's machine has static IP or DNS name -2. Firewall allows ports: 4369 (EPMD) and 9000-9100 (node communication) -3. Bootstrap peer started with `--network` flag -4. Use IP addresses or fully-qualified domain names +## Usage -**Server Machine:** +**Same machine (3 peers):** ```bash -./bin/gossip server --network +./bin/gossip alice # Terminal 1 +./bin/gossip bob alice@127.0.0.1 # Terminal 2 +./bin/gossip carol alice@127.0.0.1 # Terminal 3 ``` -**Client Machine:** +**Different machines:** ```bash -# Using IP address -./bin/gossip client server@198.51.100.42 - -# OR using domain -./bin/gossip client server@example.com +./bin/gossip alice --network # Machine 1 +./bin/gossip bob alice@machine1.local # Machine 2 (or alice@192.168.1.10) ``` -### After Running `make install` - -Works from anywhere on your system (replace `./bin/gossip` with just `gossip`): - +**After `make install`, use `gossip` directly:** ```bash -# Same machine gossip alice gossip bob alice@127.0.0.1 - -# Different machines -gossip alice --network -gossip bob alice@machine1.local ``` ## Building & Distribution @@ -288,56 +218,26 @@ This ensures all peers eventually have the same view of who's online. ## Code Organization -| File | Purpose | Lines | -|------|---------|-------| -| `src/gossip.gleam` | Entry point, CLI, input validation | 180 | -| `src/p2p_peer.gleam` | Peer startup, networking, I/O | 190 | -| `src/client.gleam` | Message display actor | 35 | -| `src/registry.gleam` | Message routing and gossip protocol | 300 | -| `src/messages.gleam` | Message type definitions | 20 | -| `src/gossip_utils.erl` | Erlang FFI utilities | 150 | -| `bin/gossip_wrapper` | Unix wrapper script | 55 | -| `Makefile` | Build system | 70 | - -Total: ~1000 lines of code + documentation - -## Extending Gossip +| File | Purpose | +|------|---------| +| `src/gossip.gleam` | Entry point, CLI, validation | +| `src/p2p_peer.gleam` | Peer startup, input handling | +| `src/client.gleam` | Message display actor | +| `src/registry.gleam` | Message routing, deduplication, gossip | +| `src/messages.gleam` | Message type definitions | +| `src/gossip_utils.erl` | Erlang FFI utilities | +| `bin/gossip_wrapper` | Unix wrapper script | -### Add Persistence +**Total**: ~1350 lines of code -Store messages in ETS (in-memory) or SQLite: -- In `registry.gleam`, add a message buffer to `RegistryState` -- When displaying ChatMessage, save to buffer -- On peer join, send message history in Gossip +## Extending -### Add Authentication - -Require passwords or cryptographic verification: -- Add `shared_secret` field to peer handshake -- In `messages.gleam`, add `secret` field to `PeerJoin` -- In `registry.gleam`, verify secret before accepting peer - -### Add Encryption - -Encrypt messages between peers: -- Use Gleam's crypto library -- Encrypt message in `propagate_broadcasts` -- Decrypt in message handler before displaying - -### Add Private Messages - -Support direct messages between specific peers: -- Add new message type: `PrivateMessage(to, from, text)` -- In registry, route to specific peer instead of all peers -- Update client to distinguish broadcast vs private - -### Scale to Many Peers - -For 100+ peers, optimize network usage: -- Switch from full mesh to partial mesh -- Use gossip for peer discovery, but don't connect to all -- Implement DHT (distributed hash table) for peer lookup -- Add message caching to prevent redundant forwarding +Possible enhancements: +- **Persistence**: Add message history storage in `registry.gleam` +- **Authentication**: Add credentials to `PeerJoin` message +- **Encryption**: Use Gleam crypto library in `propagate_broadcasts` +- **Private messages**: Add `PrivateMessage` type in `messages.gleam` +- **Partial mesh**: For many peers (100+), don't connect to all peers ## Troubleshooting @@ -420,12 +320,9 @@ Open source - use for educational and personal projects. - **BEAM VM**: [Erlang Documentation](https://www.erlang.org/doc/) - **Distributed Systems**: [Designing Data-Intensive Applications](https://dataintensive.net/) -## Next Steps to Learn More - -1. **Modify peer names**: Change how `gossip_wrapper` initializes Erlang -2. **Add custom message types**: Extend `messages.gleam` -3. **Implement heartbeats**: Add periodic ping/pong in `registry.gleam` -4. **Build a web interface**: Create HTTP server in another Gleam module -5. **Deploy to cloud**: Package with Docker or use managed Erlang platforms +## Learn More -Happy chatting! 🎉 +- **Gossip Protocols**: [Wikipedia](https://en.wikipedia.org/wiki/Gossip_protocol) +- **Erlang/OTP**: [erlang.org](https://www.erlang.org/) +- **Gleam**: [gleam.run](https://gleam.run/) +- **Distributed Systems**: [Designing Data-Intensive Applications](https://dataintensive.net/) |