secure signalling and authentication for simple-peer using firebase realtime database.
https://firebase.google.com/docs/web/setup https://firebase.google.com/docs/database/web/start
Basically, you'll need to create a firebase project and setup the JS client SDK:
firebase.initializeApp({
//values from firebase console
});
You need to configure your security rules in the console like below to secure the signalling data. What this means is only the user with uid of $uid
can access offers sent by peers (/peers/$uid/offers
) and only the peer who sent the offer can access the offer (/peers/$uid/offers/$offerId
) and the corresponding answer (/peers/$uid/offers/$offerId/answer
). This also guarantees that /peers/$uid/offers/$offerId/uid
is the uid of the user that sent the offer.
{
"rules": {
"peers": {
"$uid": {
"offers": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid",
"$offerId": {
".read": "auth != null && data.child('uid').val() == auth.uid",
".write": "auth != null && !data.exists() && newData.child('uid').val() == auth.uid",
}
}
}
}
}
}
You will also need to configure your preferred authentication method:
https://console.firebase.google.com/u/0/project/<YOUR PROJECT ID>/authentication/providers
Right now, firebase supports Email/Password, Phone, Google, Facebook, Twitter, Github, or Anonymous sign-in methods.
More details here: https://firebase.google.com/docs/auth/web/start
npm install firepeer
or
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/lib/firepeer.min.js"></script>
//alice side
const alice = new FirePeer(firebase);
//authenticate
await firebase.auth().signInWith**()
// wait for connection
const connection = await alice.connect(<uid of bob>);
// send a mesage to bob
connection.send('hello')
// bob side
const bob = new FirePeer(firebase);
//authenticate
await firebase.auth().signInWith**()
// wait for connection and receive message
bob.on('connection', (connection)=>{
connection.on('data', (data)=>{
console.log(data) //hello
})
})
Connections are just instances of SimplePeer already connected!