secure signalling and authentication for simple-peer using firebase realtime database.
Follow the instructions here:
Basically, you'll need to create a firebase project and setup the JS client SDK:
firebase.initializeApp({
//values from firebase console
});
You will also need to add these in your security rules through the console.
{
"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",
}
}
}
}
}
}
npm install firepeer
or
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/lib/firepeer.min.js"></script>
//alice side
const cred = await firebase.auth().signInWith**() //sign in with any method as alice;
const firepeer = new FirePeer({
user: cred.user,
ref: firebase.database().ref(`/users/${cred.user.uid}`)
});
// initiate connection
const connection = await alice.connect(firebase.database.ref('users/<uid of bob>'));
// send a mesage to bob
connection.send('hello')
// bob side
// create a receiver
const cred = await firebase.auth().signInWith**() //sign in with any method as bob;
const firepeer = new FirePeer({
user: cred.user,
ref: firebase.database().ref(`/users/${cred.user.uid}`)
});
// wait for connection and receive message
receiver.on('connection', (connection)=>{
connection.on('data', (data)=>{
console.log(data) //hello
})
})
When a reference is passed through new FirePeer()
, firepeer will create a child name offers
under that reference and listen to any child_added event on that node. When a reference is passed through .connect()
, firepeer will create a new child under offers
.
The rules ensure that only user with $uid, will read and write access
Use firepeer to establish a p2p connection:
Connections are just instances of SimplePeer already connected!