firepeer
Secure p2p signalling and authentication for simple-peer using firebase realtime database.

Setup firebase
-
Create a firebase project and setup the javascript client SDK.
-
Add these security rules in the firebase console to secure the signalling data.
{
"rules": {
"peers": {
"$uid": {
"$id": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid",
"$otherUid": {
"$otherId": {
".read": "auth != null && auth.uid == $otherUid",
".write": "auth != null && auth.uid == $otherUid",
"sdp": {
".validate": "newData.isString() && newData.val().length < 4000"
},
"type": {
".validate": "newData.val() == 'offer' || newData.val() == 'answer' || newData.val() == 'error'"
},
"$other": { ".validate": false }
}
}
}
}
}
}
}
Signaling data is exchanged in /peers/$uid/$id/$otherUid/$otherId. Security rules ensure that only intended users can access signalling data and signals are valid.
Shortcut: https://console.firebase.google.com/project/<YOUR_PROJECT_ID>/database/rules
-
Enable your prefered sign-in method in the firebase console. Firepeer requires authentication, so at the very least, you have to select anonymous authentication.
Shortcut: https://console.firebase.google.com/project/<YOUR_PROJECT_ID>/authentication/providers
Install
npm install --save firepeer
<script src="https://cdn.jsdelivr.net/npm/firepeer@<FIREPEER_VERSION>/lib/firepeer.min.js"></script>
Usage
firebase.initializeApp({
});
const alice = new FirePeer(firebase);
console.log(alice.id)
await firebase.auth().signInWith*()
console.log(alice.uid)
const connection = await alice.connect(BOB_UID, BOB_ID);
connection.send('hello')
firebase.initializeApp({
});
const bob = new FirePeer({
app: firebase.app()
});
console.log(bob.id)
await firebase.auth().signInWith*()
console.log(bob.uid)
bob.on('connection', (connection)=>{
connection.on('data', (data)=>{
console.log(data)
})
})
Connections are just instances of simple-peer already connected!
Reference
https://natzcam.github.io/firepeer
Demo
https://firepeer-demo.firebaseapp.com
Source:
https://github.com/natzcam/firepeer-demo
评论