The PeerJS library

PeerJS simplifies WebRTC peer-to-peer data, video, and audio calls.

PeerJS wraps the browser's WebRTC implementation to provide a complete, configurable, and easy-to-use peer-to-peer connection API. Equipped with nothing but an ID, a peer can create a P2P data or media stream connection to a remote peer.

Downloads

Setup

Include the library

<script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>

Create a peer

var peer = new Peer();

Data connections

Connect

var conn = peer.connect('another-peers-id'); // on open will be launch when you successfully connect to PeerServer conn.on('open', function(){ // here you have conn.id conn.send('hi!'); });

Receive

peer.on('connection', function(conn) { conn.on('data', function(data){ // Will print 'hi!' console.log(data); }); });

Media calls

Call

var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; getUserMedia({video: true, audio: true}, function(stream) { var call = peer.call('another-peers-id', stream); call.on('stream', function(remoteStream) { // Show stream in some video/canvas element. }); }, function(err) { console.log('Failed to get local stream' ,err); });

Answer

var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; peer.on('call', function(call) { getUserMedia({video: true, audio: true}, function(stream) { call.answer(stream); // Answer the call with an A/V stream. call.on('stream', function(remoteStream) { // Show stream in some video/canvas element. }); }, function(err) { console.log('Failed to get local stream' ,err); }); });

PeerServer

To broker connections, PeerJS connects to a PeerServer. Note that no peer-to-peer data goes through the server; The server acts only as a connection broker.

PeerServer Cloud

If you don't want to run your own PeerServer, we offer a free cloud-hosted version of PeerServer. Official PeerServer!

Run your own

PeerServer is open source and is written in node.js. You can easily run your own.

Questions?

Get started!

Please report any bugs you find on the Github issues page

Cloud hosting powered by OVH.