Enterprise-grade peer-to-peer networking library with advanced NAT traversal, end-to-end encryption, and publish-subscribe messaging. Built from the ground up in C++17 with comprehensive C, Java, and Android support.
Everything you need for robust P2P networking
ICE, STUN, and TURN support with 99%+ success rate across any network topology. Automatic strategy selection for optimal connectivity.
Noise Protocol implementation with Curve25519 + ChaCha20-Poly1305. Automatic key management and perfect forward secrecy.
Scalable publish-subscribe messaging with mesh networking. Topic-based communication and message validation.
Chunked transfers with resume capability. Directory transfer support with parallel transmission and integrity verification.
DHT integration, mDNS local discovery, and STUN-based public IP discovery. Automatic peer reconnection.
Native C++17 implementation with zero-copy operations. Thread-safe design and minimal memory footprint.
Get up and running in minutes
#include "librats.h"
#include <iostream>
#include <thread>
#include <chrono>
int main() {
// Create a simple P2P client
librats::RatsClient client(8080);
// Set up connection callback
client.set_connection_callback([](socket_t socket, const std::string& peer_id) {
std::cout << "✅ New peer connected: " << peer_id << std::endl;
});
// Set up message callback
client.set_string_data_callback([](socket_t socket, const std::string& peer_id, const std::string& message) {
std::cout << "💬 Message from " << peer_id << ": " << message << std::endl;
});
// Start the client
if (!client.start()) {
std::cerr << "Failed to start client" << std::endl;
return 1;
}
std::cout << "🐀 librats client running on port 8080" << std::endl;
// Connect to another peer (optional)
// client.connect_to_peer("127.0.0.1", 8081);
// Send a message to all connected peers
client.broadcast_string_to_peers("Hello from librats!");
// Keep running
std::this_thread::sleep_for(std::chrono::minutes(1));
return 0;
}
#include "librats.h"
#include <iostream>
int main() {
librats::RatsClient client(8080);
// Configure custom protocol for your application
client.set_protocol_name("my_app");
client.set_protocol_version("1.0");
std::cout << "Protocol: " << client.get_protocol_name()
<< " v" << client.get_protocol_version() << std::endl;
std::cout << "Discovery hash: " << client.get_discovery_hash() << std::endl;
client.start();
// Start DHT discovery with custom protocol
if (client.start_dht_discovery()) {
// Announce our presence
client.announce_for_hash(client.get_discovery_hash());
// Search for other peers using same protocol
client.find_peers_by_hash(client.get_discovery_hash(),
[](const std::vector<std::string>& peers) {
std::cout << "Found " << peers.size() << " peers" << std::endl;
});
}
return 0;
}
#include "librats.h"
#include <iostream>
#include <string>
int main() {
librats::RatsClient client(8080);
// Set up message handlers using the modern API
client.on("chat", [](const std::string& peer_id, const nlohmann::json& data) {
std::cout << "[CHAT] " << peer_id << ": "
<< data["message"].get<std::string>() << std::endl;
});
client.on("user_join", [](const std::string& peer_id, const nlohmann::json& data) {
std::cout << "[JOIN] " << data["username"].get<std::string>()
<< " joined" << std::endl;
});
// Connection callback
client.set_connection_callback([&](socket_t socket, const std::string& peer_id) {
std::cout << "✅ Peer connected: " << peer_id << std::endl;
// Send welcome message
nlohmann::json welcome;
welcome["username"] = "User_" + client.get_our_peer_id().substr(0, 8);
client.send("user_join", welcome);
});
client.start();
// Send a chat message
nlohmann::json chat_msg;
chat_msg["message"] = "Hello, P2P chat!";
chat_msg["timestamp"] = std::time(nullptr);
client.send("chat", chat_msg);
return 0;
}
int main() {
librats::RatsClient client(8080);
// Set up topic message handlers
client.on_topic_message("chat", [](const std::string& peer_id,
const std::string& topic, const std::string& message) {
std::cout << "Chat from " << peer_id << ": " << message << std::endl;
});
client.on_topic_json_message("events", [](const std::string& peer_id,
const std::string& topic, const nlohmann::json& data) {
std::cout << "Event: " << data["type"] << " from " << peer_id << std::endl;
});
// Set message validator
client.set_topic_message_validator("chat", [](const std::string& topic,
const std::string& message, const std::string& peer_id) {
// Only accept messages shorter than 1000 characters
return message.length() <= 1000 ?
librats::ValidationResult::ACCEPT :
librats::ValidationResult::REJECT;
});
client.start();
client.start_dht_discovery();
// Subscribe to topics
client.subscribe_to_topic("chat");
client.subscribe_to_topic("events");
// Publish messages
client.publish_to_topic("chat", "Hello, GossipSub world!");
nlohmann::json event_data;
event_data["type"] = "user_login";
event_data["timestamp"] = std::time(nullptr);
client.publish_json_to_topic("events", event_data);
return 0;
}
#include "librats.h"
#include <iostream>
int main() {
librats::RatsClient client(8080);
// Set up file transfer callbacks
client.on_file_transfer_progress([](const librats::FileTransferProgress& progress) {
std::cout << "\ud83d\udcc1 Transfer " << progress.transfer_id.substr(0, 8)
<< ": " << progress.get_completion_percentage() << "% complete"
<< " (" << (progress.transfer_rate_bps / 1024) << " KB/s)" << std::endl;
});
client.on_file_transfer_completed([](const std::string& transfer_id, bool success, const std::string& error) {
if (success) {
std::cout << "\u2705 Transfer completed: " << transfer_id.substr(0, 8) << std::endl;
} else {
std::cout << "\u274c Transfer failed: " << error << std::endl;
}
});
// Auto-accept incoming file transfers
client.on_file_transfer_request([](const std::string& peer_id,
const librats::FileMetadata& metadata,
const std::string& transfer_id) {
std::cout << "\ud83d\udce5 Incoming: " << metadata.filename << " from " << peer_id.substr(0, 8) << std::endl;
return true; // Auto-accept
});
client.start();
// Configure transfer settings
librats::FileTransferConfig config;
config.chunk_size = 64 * 1024; // 64KB chunks
config.max_concurrent_chunks = 4; // 4 parallel chunks
config.verify_checksums = true; // Verify integrity
client.set_file_transfer_config(config);
std::cout << "File transfer ready. Connect peers and exchange files!" << std::endl;
// Example transfers (replace "peer_id" with actual peer ID)
//std::string file_transfer = client.send_file("peer_id", "my_file.txt");
//std::string dir_transfer = client.send_directory("peer_id", "./my_folder");
//std::string file_request = client.request_file("peer_id", "remote_file.txt", "./downloaded_file.txt");
return 0;
}
Engineered for efficiency and resource optimization
Test Environment: AMD Ryzen 7 5700U, 16GB RAM
Connect across virtually any network topology
NAT Type | Direct | STUN | ICE | TURN | Success Rate |
---|---|---|---|---|---|
Open Internet | ✅ | ✅ | ✅ | ✅ | 100% |
Full Cone NAT | ❌ | ✅ | ✅ | ✅ | 95% |
Restricted Cone | ❌ | ✅ | ✅ | ✅ | 90% |
Port Restricted | ❌ | ✅ | ✅ | ✅ | 85% |
Symmetric NAT | ❌ | ❌ | ⚠️ | ✅ | 70% |
Double NAT | ❌ | ❌ | ❌ | ✅ | 99% |
Get the latest version for your platform
Windows 10/11 (x64)
macOS 10.15+ (Universal)
Ubuntu 18.04+ / CentOS 7+
Compile librats yourself for maximum compatibility
git clone https://github.com/DEgITx/librats.git
cd librats
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
Cross-platform support with bindings for multiple programming languages
Production-ready implementations across all major platforms
Platform | Build Environment | Compiler | Status |
---|---|---|---|
Windows
|
MinGW-w64 | GCC 7+ | Fully Supported |
Windows
|
Visual Studio | MSVC 2017+ | Fully Supported |
Linux
|
Native | GCC 7+, Clang 5+ | Fully Supported |
macOS
|
Xcode/Native | Clang 10+ | Fully Supported |
Multi-language support for diverse development ecosystems
Language/Platform | Binding Type | Status | Timeline | Notes |
---|---|---|---|---|
C/C++
|
Native Library | Fully Supported | Available Now | Core implementation with full feature set |
Android (NDK)
|
Native C++ | Fully Supported | Available Now | Android NDK integration with JNI bindings |
Android (Java)
|
JNI Wrapper | Fully Supported | Available Now | High-level Java API for Android apps |
JavaScript
|
Native Module | Planned | Soon | Node.js native addon with async/await support |
Python
|
C Extension | Planned | Soon | CPython extension with asyncio integration |
Rust
|
FFI Bindings | Planned | Soon | Safe Rust bindings with tokio async support |
Go
|
CGO Bindings | Future | Soon | CGO wrapper for Go applications |
C#/.NET
|
P/Invoke | Future | Soon | .NET bindings for Windows/Linux/macOS |
Native mobile development for iOS and Android
NDK + JNI Implementation
Native C++ Implementation
Web and desktop application development
WebAssembly Implementation
Node.js Module
Rust Bindings
Everything you need to get started and build amazing P2P applications
Complete documentation for NAT traversal configuration and optimization
Efficient P2P file and directory transfer with resume capability
Event-driven messaging system with JSON support and callbacks
Publish-subscribe messaging with mesh networking and topic routing
End-to-end encryption details and key management
Configure custom protocols and discovery mechanisms