A comprehensive Node.js CLI application demonstrating Hyperswarm features implementing a real-time chat application.
- π Peer-to-Peer Discovery: Automatic peer discovery using Hyperswarm DHT
- π¬ Real-time Messaging: Interactive chat with connected peers
- π‘ Broadcasting: Send messages to all connected peers
- π Ping/Pong: Network latency testing between peers
- π Connection Management: Track and manage peer connections
- π Comprehensive Logging: Detailed logs with color-coded output
- ποΈ Peer-to-Peer Mode: Direct peer connections with automatic discovery
- β‘ Interactive CLI: Rich command interface with help system
# Clone and install
git clone <your-repo>
cd hyperswarm-cli-example-app
npm install# Start as a peer (generates random topic)
npm run peer
# Run automated demo
npm run demonode main.js [options]
Options:
-m, --mode <mode> Operation mode: peer (default: peer)
-t, --topic <topic> Hex-encoded topic to join (generates random if not provided)
-n, --name <name> Peer name (generates random if not provided)
-p, --port <port> Port number (for future use)
-h, --help Show help message
-v, --version Show version informationOnce running, use these commands in the CLI:
/help- Show available commands/peers- List connected peers/ping- Ping all connected peers/broadcast <message>- Broadcast message to all peers/history- Show message history/status- Show connection status/quit- Quit the application<message>- Send message to all peers
Terminal 1 (First Peer):
npm run peer
# Note the topic from the outputTerminal 2 (Second Peer):
node main.js --topic <TOPIC_FROM_FIRST_PEER>Terminal 1:
node main.js --name "Alice" --topic "deadbeefcafebabe1234567890abcdef1234567890abcdef1234567890abcdef"Terminal 2:
node main.js --name "Bob" --topic "deadbeefcafebabe1234567890abcdef1234567890abcdef1234567890abcdef"After connecting peers, try these commands:
> Hello everyone!
> /ping
> /peers
> /broadcast This is a broadcast message
> /status
> /help
| Script | Description |
|---|---|
npm start |
Start the application |
npm run peer |
Start as peer mode |
npm run demo |
Run automated demo with multiple peers |
npm test |
Run test suite |
npm run logs |
Monitor log files |
npm run clean |
Clean log files |
Each peer creates its own log file:
<peer-name>.log- Contains detailed logs for each peer- Use
npm run logsto monitor all log files in real-time - Use
npm run cleanto remove old log files
The application uses color-coded logging:
- π΅ INFO - General information
- π’ SUCCESS - Successful operations
- π‘ WARN - Warnings
- π΄ ERROR - Errors
- β« DEBUG - Debug information
- π£ PEER - Peer-related events
- π· CONNECTION - Connection events
- HyperswarmCLI Class - Main application class
- Logger - Comprehensive logging system
- Connection Manager - Handles peer connections
- Message System - Processes different message types
- CLI Interface - Interactive command system
welcome- Initial greeting between peerschat- Regular chat messagesbroadcast- Messages sent to all peersping/pong- Network latency testing
- Application starts and joins Hyperswarm topic
- DHT discovers other peers on the same topic
- Connections are established automatically
- Peers can exchange messages interactively
- Connection events are logged and tracked
npm testThis runs a comprehensive test that:
- Starts multiple peer instances
- Tests peer discovery and connection
- Verifies message exchange
- Monitors for errors
- Cleans up automatically
-
Single Machine Multi-Terminal:
# Terminal 1 npm run peer # Terminal 2 npm run peer # Terminal 3 npm run peer
-
Network Testing:
- Run on different machines using same topic
- Test NAT traversal and holepunching
- Monitor connection establishment
# Enable debug mode
DEBUG=hyperswarm* node main.js
# Custom storage location
HYPERSWARM_STORAGE=/custom/path node main.jsThe application is designed to be easily extensible:
- Add Message Types: Extend the message handling system
- Custom Protocols: Implement additional protocols
- Storage Integration: Add persistence capabilities
- UI Enhancements: Improve the CLI interface
hyperswarm-cli-hello/
βββ main.js # Main application
βββ logger.js # Logging system
βββ test.js # Test suite
βββ package.json # Dependencies and scripts
βββ README.md # This file
βββ *.log # Generated log files
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if needed
- Submit a pull request
MIT License - see LICENSE file for details.
- Hyperswarm - The core P2P networking library
- Hypercore - Distributed append-only log
- Hyperbee - Distributed key-value store
For questions or issues:
- Check the logs with
npm run logs - Run tests with
npm test - Enable debug mode for detailed output
- Create an issue with logs and reproduction steps
Made with β€οΈ using Hyperswarm - Readme authored with Claude