Lightweight library that will let you create a p2p network with ease.
const createp2pnode = require('swenssonp2p');
const node = createp2pnode();
// Start listening to connections
node.listen(PORT, () => {
console.log('Listening to connections...');
});
// Connect to other nodes
node.connect(IP, PORT, () => {
console.log('Connected to some other node.');
});
// Send message to everyone
// data can be anything that
// you can JSON.stringify
node.broacast(data);
// Send message to specific node
// data can be anything that
// you can JSON.stringify
node.direct(recipientId, data);
// Some other node has connected to
// us (neighbor)
node.on('connect', ({ nodeId }) => {
console.log('Node', nodeId, 'has connected');
});
// Neighbor has disconnected
node.on('disconnect', ({ nodeId }) => {
console.log('Node', nodeId, 'has disconnected');
});
// Some message has been broadcasted somewhere
// on the network and has reached us
node.on('broadcast', ({ origin, message }) => {
console.log('Message', message, 'has been broadcasted from', origin);
});
// Some message has been sent to us
node.on('direct', ({ origin, message }) => {
console.log('Message', message, 'has been directly send to us from', origin);
});
// Shut down node
node.close(() => {
console.log('Node is down');
});
Run chat application with node examples/chat PORT
. If you want to test it locally, run several instances of the chat and connect them using connection IP:PORT
to any other instances, creating a p2p network. Send a message simply typing anything in the console.