Clap Peer is a versatile library designed for creating distributed networks where nodes can seamlessly exchange messages. With support for both plain and encrypted communications, it employs advanced methods like RSA and AES for secure data transfer. Ideal for developers looking to implement robust P2P communication in their applications.
Clap Peer is an innovative library designed for establishing TCP-based peer-to-peer (P2P) networks for seamless data transmission. It enables nodes within the network to exchange messages, whether plain or encrypted, leveraging advanced cryptographic techniques to ensure secure communication.
Integrating Clap Peer into your application is straightforward. Here's a simple example demonstrating how to set up nodes and send messages:
const { ClapPeer, DM, CRYPTO_DM, ERROR } = require('clap-peer');
//Node - 1 Setup
const node_1 = new ClapPeer(1001, 'A');
node_1.on(DM, msg => console.log('Received plain message:', msg));
node_1.on(CRYPTO_DM, msg => console.log('Received encrypted message:', msg));
//Node - 2 Setup
const node_2 = new ClapPeer(1002, 'B');
node_2.connect({ host: '127.0.0.1', port: 1001 });
node_2.send(node_1.nodeId, { hello: 'hello crypto' }).catch(error => console.log('Error sending:', error));
node_2.publish(node_1.nodeId, { hello: 'just hello' });
You can connect to other nodes in the network using one of two methods to fit your code's architecture:
Using the .connect() Method:
const node = new ClapPeer(1001, 'A');
node.connect({ host: '127.0.0.1', port: 1002 });
This method separates node creation and connection, allowing for clearer management of logic.
Using Configuration During Node Creation:
const node = new ClapPeer(1002, 'A', { host: '127.0.0.1', port: 1002 });
This is ideal for immediate connections upon node setup.
Clap Peer provides two methods for sending messages:
send() — For Encrypted Messages:
node.send(node_2.nodeId, { text: 'Hello, secure world!' }).catch(error => console.log('Error:', error));
The send method handles encryption and checks for available public keys, ensuring secure data transmission.
publish() — For Plain Messages:
node.publish(node_2.nodeId, { text: 'Hello, open world!' });
The publish method propagates messages without encryption directly to the specified node.
| Method | Encryption | Public Key Check | Forwarding Through Nodes |
|---|---|---|---|
send | ✅ | ✅ | ✅ |
publish | ❌ | ❌ | ✅ |
Nodes can respond to incoming messages by subscribing to specific events:
DM for plain messages:
node.on(DM, msg => console.log('Plain message received:', msg));
CRYPTO_DM for encrypted messages:
node.on(CRYPTO_DM, msg => console.log('Encrypted message received:', msg));
| Event | Method That Creates Message | Message Type |
|---|---|---|
DM | publish | Plain text message |
CRYPTO_DM | send | Encrypted message |
To keep your application robust, Clap Peer allows you to handle errors gracefully by subscribing to the ERROR event:
node.on(ERROR, (messageError, originalError) => {
console.log('Message error:', messageError);
console.error('Original error:', originalError);
});
Incorporating Clap Peer into your next project will empower secure and efficient P2P data transmission, enhancing communication protocols within your application.
No comments yet.
Sign in to be the first to comment.