Home

Awesome

<a href="http://unsetbit.com/p">P</a> is for peer-to-peer networking with browsers

P is a small framework used to create browser-to-browser networks (as opposed to just a connection). With P, you can:

After a connection is established the middleman is no longer necessary – no proxies are involved.

This is made possible by an unstable and young technology -- WebRTC. Currently, only Chrome and Firefox support this technology.

onramp, a simple WebSocket server, is used as the signaling channel to establish initial connections.

API

// Initializing
var rootNode = P.create(); // create the root node

// Connection management
var webSocketNode = rootNode.connect(address); // connect to an onramp WebSocket server
var webRtcNode = webSocketNode.connect(address); // connect to a peer using an onramp connection
var webRtcNode = webRtcNode.connect(address); // connect to a peer using an existing peer connection
anyNode.close(); // close the connection
anyNode.isOpen(); // return true if the connection is open
var nodeArray = anyNode.getPeers(); // returns an array of all peer connections

// Firewalling connections
var protectedNode = P.create({
  firewall: function(offerData){
    // Only accept RTC connection offers which send 'secret' as the offer data
    // this firewall rule will apply to any child nodes as well
    return offerData === 'secret';
  }
});


// Send offerData with a connection request
anyNode.connect({address: address, offerData: 'secret'});


// Sending and receiving messages
webRtcNode.send(message); // send a message to a peer; can be json, string, or arraybuffer
webRtcNode.on('message', function(message){}); // listens for messages from a peer
webRtcNode.on('array buffer', function(arrayBuffer){}); // listens for array buffers from a peer

// Events
anyNode.on('connection', function(peerNode){}); // emitted when a connection is made via this peer
anyNode.on('open', function(){}); // emitted when this connection is open and ready
anyNode.on('close', function(){}); // emitted when this connection is closed
anyNode.on('error', function(err){}); // listens for errors for this connection
anyNode.removeListener(eventName, optionalCallback); // stops listening to an event

Documentation

Release Notes