Comments:"PeerJS: Simple peer-to-peer data in the browser with WebRTC"
PeerJS — True peer-to-peer data in the browser
Peer
<script>
var peer = new Peer('someid', {key: 'apikey'});
peer.on('connection', function(conn) {
conn.on('data', function(data){
// Will print 'hi!'
console.log(data);
});
});</script>
Connecting peer
<script>
var peer = new Peer('anotherid', {key: 'apikey'});
var conn = peer.connect('someid');
conn.on('open', function(){
conn.send('hi!');
}); </script>
The PeerJS library
PeerJS completes WebRTC as an API abstraction, connection broker, and binary serialization format
The WebRTC specification leaves several questions unanswered.
It's up to the developer to create a back channel to exchanges session offer/answer, ICE candidates, and provide identity. PeerJS aims to fill holes.
PeerJS wraps the WebRTC implementation to provide a complete, configurable, and easy-to-use peer-to-peer data API. Each peer simply provides a identifier with which other peers using the same API key can connect.
PeerJS Features
- Simple API – make a peer connection in just 3 lines of code
- Deals with WebRTC handshake
- Allows connections to peer by id
- Optimized connection brokering latency – using hybrid XHR + WS
- True binary data support with BinaryPack serialization
- "Preconnect" to clients before they connect for fastest connection establishment
- True peer-to-peer
- Handles NAT traversals
- Backed by UDP
- Configurable data reliability
- Supports raw binary data
WebRTC Features
PeerServer
To broker connections, PeerJS connects to PeerServer. Note that no peer-to-peer data goes through the server; The server acts only to broker connections.
PeerServer Cloud
If you don't want to run your own PeerServer, we have a free hosted version of PeerServer available for your use.
Get a PeerServer Cloud API Key
Run your own
PeerServer is open source and written in Node. You can easily run your own.
Other
PeerJS is still a work in progress so there are sure to be bugs.
Please report them on the Github issues page
If you have questions, feel free to email at team@peerjs.com