web-peer
Easy way to use WebRTC
What can do
- Send
MediaStreams
(audio, video) - Send messages
- Send files (< 200mb for now)
Install
npm install web-peer
How to use
Web-peer provide only client-side interface for WebRTC. You should use some signaling mechanism.
Example of using with socket.io:
var socket = Peer = peer = ; peer;peer;socket;
and server.js
var io = require('socket.io');
io.sockets.on('connection', function (socket) {
socket.on('sync', function (data) {
socket.broadcast.emit('sync', data);
});
});
io.listen(<your port>)
API
Peer()
creates new peer
var peer = ;
Peer#sync(options)
Two peer should sync with some signaling mechanism. This is only function needed for that.
Any peer emit sync
event when it require to signale to another peer
peer;
another peer waing until sync
function will be callen
;
Peer#send(messageName, messageData)
send message
peer;
on another side
peermessages;
Peer#addStream(mediaStream)
send MediaStream
;
on another side
peer;
Peer#sendFile(file)
send File
var input = document; input;
on another side
// file is instanceof private constructor FileStream
which makes easier work with file
peer.on('file', function (file) {
// file will load only after you allow it
file.load();
file.on('progress', function (progress) {
document.querySelector('progress').value = progress;
});
file.on('url', function () {
var link = document.querySelector('a');
a.download = file.name;
a.innerHTML = file.name;
a.href = file.url;
});
});
Peer#close()
close connection
Peer events
error
any errorstream
new incomingMediaStream
file
new incoming filesync
request to use signaling mechanism to send data to another peerclose
when connection is closeddisconnect
when remote peer is disconnected
FileStream#load()
start load file
FileStream#getBlob()
get file blob
FileStream#url
url to filesystem file location
FileStream#name
file name