Home >Web Front-end >JS Tutorial >TCP-based Pdata transmission
ClapPeer - is a library for creating a distributed network of nodes that supports message exchange between them. Nodes can exchange both plain and encrypted messages.
https://github.com/DIY0R/clap-peer
const { ClapPeer, DM, CRYPTO_DM, ERROR, INVALID_CRYPTO_DM, } = require('clap-peer'); //Node - 1 const node_1 = new ClapPeer(1001, 'A'); node_1.on(DM, msg => console.log(msg)); node_1.on(CRYPTO_DM, msg => console.log(msg)); //Node - 2 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)); node_2.publish(node_1.nodeId, { hello: 'just hello' });
You can use either of two methods to connect to a node: via the .connect method or by passing a configuration object when creating the node. Choose the method based on how you want to structure your code. Here are the two approaches:
const node = new ClapPeer(1001, 'A'); node.connect({ host: '127.0.0.1', port: 1002 });
Here, we create a node and then call the .connect() method, passing the host and port parameters. This allows you to separate the node creation and connection logic.
const node = new ClapPeer(1002, 'A', { host: '127.0.0.1', port: 1002 });
In this case, we pass the connection parameters directly when creating the ClapPeer object. This method is convenient if you need to connect to the node immediately upon creation.
The send method is used to send encrypted messages. Before sending, it checks if the target node's public key is available:
Example:
node.send(node_2.nodeId, { text: 'Hello, secure world!' }).catch(error => { console.log(error); });
The publish method sends messages without encryption. It simply forwards the data to the specified node.
Example:
node.publish(node_2.nodeId, { text: 'Hello, open world!', });
Method | Encryption | Public Key Check | Request Public Key if Needed | Routing Through Intermediate Nodes |
---|---|---|---|---|
send | ✅ | ✅ | ✅ | ✅ |
publish | ❌ | ❌ | ❌ | ✅ |
A node can subscribe to events to handle both plain and encrypted messages.
DM type messages are generated when another node calls the publish method. These messages are transmitted unencrypted.
const { ClapPeer, DM, CRYPTO_DM, ERROR, INVALID_CRYPTO_DM, } = require('clap-peer'); //Node - 1 const node_1 = new ClapPeer(1001, 'A'); node_1.on(DM, msg => console.log(msg)); node_1.on(CRYPTO_DM, msg => console.log(msg)); //Node - 2 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)); node_2.publish(node_1.nodeId, { hello: 'just hello' });
Messages of type CRYPTO_DM are generated when the send method is called by another node. These messages are received in encrypted form.
const node = new ClapPeer(1001, 'A'); node.connect({ host: '127.0.0.1', port: 1002 });
Event | Method That Generates the Message | Message Type | Description |
---|---|---|---|
DM | publish | Plain text message | Handled as a regular, unencrypted message. |
CRYPTO_DM | send | Encrypted message | Sent and received in an encrypted form. |
You can subscribe to the ERROR event to handle all errors.
const node = new ClapPeer(1002, 'A', { host: '127.0.0.1', port: 1002 });
The messageError.message parameter may contain one of the following messages:
TIMEOUT_ERROR_MESSAGE:
Neighbor check timed out after ${TIMEOUT_DURATION / 1000} seconds
TIMEOUT_ERROR_REQUEST:
RSA key retrieval timed out.
DECRYPT_ERROR:
Unable to decrypt the message.
SEND_ERROR:
Failed to send the data.
PUBLISH_ERROR:
Failed to publish the data.
The above is the detailed content of TCP-based Pdata transmission. For more information, please follow other related articles on the PHP Chinese website!