peerjs / 0.3.8 last updated 4 months ago created on Mar 25th 2013

Install

npm install --save peerjs

Clone

git clone git@github.com:peers/peerjs.git

dependencies

No dependencies listed.

maintainers

michelle
ericz

versions 7 total

0.3.8 4 months ago michelle
0.3.6 7 months ago michelle
0.3.5 7 months ago michelle
0.3.4 8 months ago michelle
0.3.2 8 months ago michelle
0.3.1 8 months ago michelle
0.2.0 a year ago michelle

readme

PeerJS: Simple peer-to-peer with WebRTC

PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built on top of WebRTC, supporting both data channels and media streams.

http://peerjs.com

Setup

Include the library

<script src="http://cdn.peerjs.com/0.3/peer.js"></script>

Create a Peer
Get a free API key. Your id only needs to be unique to the namespace of your API key.

var peer = new Peer('pick-an-id', {key: 'myapikey'}); 
// You can pick your own id or omit the id if you want to get a random one from the server.

Data connections

Connect

1
2
3
4
var conn = peer.connect('another-peers-id');
conn.on('open', function(){
  conn.send('hi!');
});

Receive

1
2
3
4
5
6
peer.on('connection', function(conn) {
  conn.on('data', function(data){
    // Will print 'hi!'
    console.log(data);
  });
});

Media calls

Call

1
2
3
4
5
6
7
8
9
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
navigator.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> element.
  });
}, function(err) {
  console.log('Failed to get local stream' ,err);
});

Answer

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
peer.on('call', function(call) {
  navigator.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> element.
    });
  }, function(err) {
    console.log('Failed to get local stream' ,err);
  });
});

Links

Documentation / API Reference

WebRTC Browser compatibility status

PeerServer

Discuss PeerJS on our Google Group

Changelog

Resolving all dependencies.. This could take a while as our cache is not yet fully warmed up.