Imagine a network where a node needs to know only one other peer, and from that single connection, it discovers and links to every other node. Eventually, the node will be connected to all other nodes and the other nodes will eventually be connected to this node. This is the foundation of PeerManager—a self-expanding, decentralized WebSocket-based network that is resilient, autonomous, and infinitely scalable.
At its core, each node in this network plays a dual role:
- WebSocket Server: Accepts connections from new peers.
- WebSocket Client: Actively connects to known peers.
This creates a mesh network where every node can reach every other node, ensuring high redundancy, auto-healing connections, and seamless peer discovery—all without a central authority.
A node starts with a single known peer, but the moment it joins the network:
- It broadcasts its presence.
- It receives a list of other known peers.
- It connects to them dynamically, further expanding the web.
In essence, a single connection is enough to snowball into a fully connected network.
This bidirectional connectivity is what makes the system so powerful. A node doesn’t just wait to be discovered—it actively reaches out, constantly expanding the network without relying on a central registry.
Nodes communicate using structured events, enabling flexible messaging, peer management, and future extensibility. The system can be adapted for secure key storage, real-time messaging, decentralized databases, and more.
If a peer disconnects, the network adapts:
- Other nodes attempt to reconnect.
- If a connection is lost, peers are reintroduced dynamically.
- The system remains functional even if multiple nodes go offline.
Some potential applications include:
- Nodes store shards of a cryptographic key, split using Shamir’s Secret Sharing (SSS).
- A threshold of nodes must cooperate to reconstruct the original key.
- Ideal for secure password management, cryptocurrency wallets, and sensitive data storage.
- A serverless chat network where messages propagate directly between users.
- No single point of failure, ensuring privacy and resilience.
- Quickly connects blockchain nodes without relying on static lists.
- Facilitates transaction relay, block propagation, and state synchronization.
- IoT devices automatically discover and communicate without a central controller.
- Data is shared, processed, and stored across the entire network.
const peerManager = new PeerManager("ws://localhost:8080");- Starts a WebSocket server that listens for incoming connections.
client.send(
JSON.stringify({
event: "KNOWN_PEERS",
data: {
value: this.peers.map((p) => p.peerUrl).concat(this.current_node_url),
},
})
);- When a node connects, it shares its list of known peers, accelerating network expansion.
this.broadcast("REQUEST_KNOWN_PEERS", { requester: this.current_node_url });- Periodically asks for known peers, ensuring constant growth and reconnection.
if (!isPeerEvent(parsedData)) {
console.warn("Received invalid peer event:", parsedData);
return;
}
const handler = this.eventHandlers.get(event);
if (handler) {
handler(client, data);
} else {
console.warn(`No handler registered for event: ${event}`);
}- Validates messages and routes them to appropriate handlers.
setTimeout(() => this.addPeer(peerUrl), 5000);- If a peer goes offline, a reconnection attempt is made after 5 seconds, ensuring self-healing connections.
- A node only needs one known peer to eventually reach the entire network.
- No manual configuration required.
- Peers dynamically discover, connect, and maintain links without central servers.
- The network grows naturally over time.
- If nodes drop off, others fill in the gaps.
- Automatic reconnections make the system highly resilient to failures.
- The event-based architecture makes it easy to add new functionality.
- Can be adapted for various decentralized applications beyond peer management.
- Implement end-to-end encryption for private messaging.
- Introduce peer authentication to prevent Sybil attacks.
- Optimize peer discovery with Distributed Hash Tables (DHTs).
- Introduce a hierarchical topology for better large-scale performance.
- Implement exponential backoff for reconnections.
- Store peer data persistently to avoid excessive network overhead.
- Use cryptographic signatures to authenticate messages.
- Implement checksum validation to detect tampered data.
This system isn’t just a WebSocket peer manager—it’s the blueprint for next-generation decentralized applications. Whether for secure key management, real-time messaging, IoT networking, or blockchain node discovery, PeerManager unlocks the true potential of decentralized networking.
By refining its security, scalability, and efficiency, this framework can power unstoppable, self-healing, and infinitely scalable networks—shaping the future of decentralized computing. 🚀
- Nodes automatically discover and connect to each other without needing a central server.
- Uses a gossip protocol to share peer lists and expand the network dynamically.
- Nodes can send and receive arbitrary JSON-encoded messages.
- You define the events (e.g.,
FILE_UPLOAD,CHAT_MESSAGE) and the payloads (e.g., file data, text).
- You write the business logic for your specific use case (e.g., file storage, AI training).
- The library handles the networking layer, so you can focus on what makes your app unique.
- Events are the actions your nodes can perform (e.g.,
FILE_UPLOAD,FILE_DOWNLOAD). - Each event has a JSON payload that contains the necessary data (e.g., file name, file content).
Example:
{
event: "FILE_UPLOAD",
data: {
fileName: "example.txt",
fileContent: "Hello, world!"
}
}Use registerEvent to define how your nodes should respond to specific events.
Example:
manager.registerEvent("FILE_UPLOAD", (peer, data) => {
console.log(`Received file: ${data.fileName}`);
saveFileToDisk(data.fileName, data.fileContent);
});Use broadcast to send events to all connected peers.
Example:
manager.broadcast("FILE_UPLOAD", {
fileName: "example.txt",
fileContent: "Hello, world!",
});Nodes automatically discover and connect to each other using the KNOWN_PEERS and REQUEST_KNOWN_PEERS events.
Let’s say you want to build a P2P file storage system where users can upload and download files. Here’s how you’d use the Mesh Protocol library:
FILE_UPLOAD: Upload a file to the network.FILE_DOWNLOAD: Request a file from the network.FILE_RESPONSE: Send the requested file back to the requester.
// Handle file uploads
manager.registerEvent("FILE_UPLOAD", (peer, data) => {
saveFileToDisk(data.fileName, data.fileContent);
console.log(`File saved: ${data.fileName}`);
});
// Handle file download requests
manager.registerEvent("FILE_DOWNLOAD", (peer, data) => {
const fileContent = readFileFromDisk(data.fileName);
peer.send(
JSON.stringify({
event: "FILE_RESPONSE",
data: { fileName: data.fileName, fileContent },
})
);
});// Upload a file
manager.broadcast("FILE_UPLOAD", {
fileName: "example.txt",
fileContent: "Hello, world!",
});
// Download a file
manager.broadcast("FILE_DOWNLOAD", {
fileName: "example.txt",
});Start multiple nodes and watch them discover each other and exchange files.
Use the examples folder in the repository as a starting point.
- No central servers – nodes connect directly to each other.
- Resilient to failures – the network heals itself if nodes go offline.
- Add new events and business logic without modifying the core library.
- Perfect for building custom P2P applications.
- Built on WebSocket for low-latency communication.
- Minimal overhead – ideal for resource-constrained devices.
npm install mesh-protocolThe examples folder contains template code to help you get started.
Use it as a foundation for your own project.
- Decide on the events your nodes will use.
- Write the handlers for those events.
Start multiple nodes and watch them discover each other and exchange messages.
- Decentralized File Storage: Share files across a P2P network.
- P2P Messaging: Send messages directly between users.
- Distributed AI: Train models across multiple devices.
- IoT Networks: Connect and manage smart devices.
- Blockchain Node Discovery: Sync blockchain data without central servers.
- Explore the Examples: Dive into the
examplesfolder to see the library in action. - Define Your Events: Decide what actions your nodes will perform.
- Build Your App: Write the business logic and start your network.
The Mesh Protocol library is your gateway to building decentralized, scalable, and resilient networks. What will you create? 🚀