README
Beautiful-p2p
A peer-to-peer module finished in beautiful British Columbia.
Usage
let beautiful_p2p = require('beautiful_p2p').beautiful_p2p
let bp = beautiful_p2p('localhost',4321)
bp.event_center.on('newBroadcast', function(data){
console.log(data)
})
bp.serve()
process.stdin.on('data', (data)=>{
console.log(data.toString('utf8'))
bp.broadcast(data.toString('utf8'))
})
APIs
beautiful_p2p(localhost, localport)
- localhost
IP of your server - localport
port to be used - callback
optional, callback after connecting to a new node - argu
optional, arguments of callback
Properties
- sleeping_peers
a queue to keep 10 known peers, not necessary connected now but has been known as active nodes. Adding new peers results old peers being deleted. - active_peers
a stack to keep up to 6 connected peers. The attempts adding new peers to a full stack will be rejected. - checklist
record recent 5 messages - name
name of this node - server <net.Server> server of this node
Methords
- serve():void start the server. Must be called.
- connect(config, callback, argu):void set up a new connection
- config
- callback
optional, callback function - argu
optional, arguments for callback function
- check_connection(): void check if more peers are needed. Automatically called every 15s.
- broadcast (msg): void broadcast a new message
- msg
the message you want to broadcast.
- msg
- whisper(who, data): void send a message
- who
the name of the peer, like '{"host":"192.168.1.2","port":4321}' - msg
the message to be sent
- who
- show_peers(): Array return an array of peers' names
Events
All events comes from events property
newBroadcast litsen to this event if you want to get information broad-casted in the network
- msg
information being broadcasted
- msg
newConnection triggered when a new connection was set up
- who
ip and port of the peer being connected
- who
error emitted when an error is detected
- msg
error information
- msg
server_ready emitted when the server is ready
server_close emitted when the server is about to close
whisper emitted when received a whisper