PeerConnection
What is this?
A tiny browser module that gives normalizes and simplifies the API for WebRTC peer connections.
It gives us a cleaner (cross-browser) way to handle offer/answer and is based on an Event Emitter.
If you're not using browserify or you want AMD support use rtcpeerconnection.bundle.js
.
Installing
npm install rtcpeerconnection
How to use it
Instantiation
Instantiation takes the same options as a normal peer connection constructor:
var PeerConnection = require('rtcpeerconnection');
var pc = new PeerConnection({servers}, {constraints});
Events
Unlike stock Peer Connections this is an event emitter. Powered by WildEmitter which has a very familiar API if you're used to node.js/jQUery/Backbone but also includes a wildcard handler so you can easily debug events. Just do emitter.on('*')
to log them out or whatnot.
But instead of doing pc.onicecandidate = function () {}
on a peer connection you listen for events like this:
pc.on('ice', function (candidate) {
connection.send('ice', candidate);
});
pc.on('endOfCandidates', function () {
});
pc.on('streamAdded', function (stream) {
});
pc.on('streamRemoved', function (stream) {
});
pc.on('answer', function (answer) { ... });
pc.on('offer', function (offer) { ... });
pc.on('close', function () { ... });
Methods
The whole offer/answer cycle looks like this:
var pc = new PeerConnection(servers, constraints);
var connection = new RealTimeConnection();
pc.offer(function (offer) {
connection.send('offer', offer)
});
pc.offer({
mandatory: {
OfferToReceiveAudio: true,
OfferToReceiveVideo: false
}
},
function (offer) {
}
);
connection.on('offer', function (offer) {
pc.answer(offer, function (answer) {
connection.send('answer', answer);
});
pc.answer(offer, MY_CONSTRAINTS, function (answer) {
connection.send('answer', answer);
});
pc.answerVideoOnly(offer, function (answer) { ... });
pc.answerAudioOnly(offer, function (answer) { ... });
});
connection.on('answer', function (answer) {
pc.handleAnswer(answer);
});
pc.on('ice', function (candidate) {
connection.send('ice', candidate);
});
connection.on('ice', function (candidate) {
pc.processIce(candidate);
});
That's it!
More
If you want higher level functionality look at SimpleWebRTC that uses this library.
License
MIT
Created By
If you like this, follow: @HenrikJoreteg on twitter.