Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

branchseer/n2n

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#A Node.js P2P Network

##Starting a seed One or several seed servers are required to bootstrap the network. To start a seed server listening 6785:

var Seed = require('n2n').Seed;

var seed = new Seed();
seed.listen(6785);
console.log('Seed listening 6785...');

##Starting a node The following code snippet starts a node by connecting to the seed server seed.com:6785. If the node has a public IP, it will start a seed server listening 6785, as well.

var Node = require('n2n').Node;
var node = new Node(6785);
node.connect([{ host: 'seed.com', port: 6785 }]);

Once the node gets online, the event online will be emitted.

node.on('online', function () {
  console.log('I am online:', this.id);
});

Event node::online means another node gets online. A object representing the new node will be passed to event handlers:

node.on('node::online', function (newNode) {
  console.log('Someone is online:', newNode.id);
});

##Communication Nodes communicate via events.
###Node#send(targetId:string, eventName:string, [data:]) Emit a custom event called eventName on a specific node. ###Node#broadcast(eventName:string, [data:]) Emit a custom event called eventName on all nodes online.

###Handling custom events The name of custom events must be prefixed with 'node::'. If any, event data will be passed to event handlers:

node.on('node::hello', function (senderId) {
  console.log('Hello from', senderId);
});

About

a Node.js P2P network

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published