-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nathaniel Camomot
committed
Nov 18, 2018
1 parent
4b65dab
commit e868d61
Showing
12 changed files
with
274 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,93 @@ | |
firepeer | ||
======== | ||
|
||
simple-peer signalling through firebase | ||
secure signalling and authentication for [simple-peer](https://github.com/feross/simple-peer) using [firebase realtime database](https://firebase.google.com/docs/database/). | ||
|
||
## setup firebase | ||
|
||
Follow the instructions here: | ||
1. https://firebase.google.com/docs/web/setup | ||
2. https://firebase.google.com/docs/database/web/start. | ||
|
||
Basically, you'll need to create a firebase project and setup the JS client SDK: | ||
```javascript | ||
firebase.initializeApp({ | ||
//values from firebase console | ||
}); | ||
``` | ||
|
||
You will also need to add these in your [security rules](https://firebase.google.com/docs/database/security) through the [console](https://console.firebase.google.com). | ||
|
||
```javascript | ||
{ | ||
"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", | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## install firepeer | ||
```sh | ||
npm install firepeer | ||
``` | ||
or | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/lib/firepeer.min.js"></script> | ||
``` | ||
|
||
## use firepeer | ||
```javascript | ||
//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') | ||
``` | ||
```javascript | ||
// 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 | ||
}) | ||
}) | ||
``` | ||
## how this works? | ||
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](https://github.com/feross/simple-peer#api) already connected! | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
test cleanup | ||
test non-happy path | ||
test non-happy path | ||
validation rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import * as debug from 'debug'; | ||
import debug from 'debug'; | ||
|
||
export default debug('firepeer'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
import * as firebasemock from 'firebase-mock'; | ||
// import * as firebasemock from 'firebase-mock'; | ||
|
||
const mockauth = new firebasemock.MockAuthentication(); | ||
const mockdatabase = new firebasemock.MockFirebase(); | ||
const mockfirestore = new firebasemock.MockFirestore(); | ||
const mockstorage = new firebasemock.MockStorage(); | ||
const mockmessaging = new firebasemock.MockMessaging(); | ||
const firebase = new firebasemock.MockFirebaseSdk( | ||
// use null if your code does not use RTDB | ||
(path: string) => { | ||
return path ? mockdatabase.child(path) : mockdatabase; | ||
}, | ||
// use null if your code does not use AUTHENTICATION | ||
() => { | ||
return mockauth; | ||
}, | ||
// use null if your code does not use FIRESTORE | ||
() => { | ||
return mockfirestore; | ||
}, | ||
// use null if your code does not use STORAGE | ||
() => { | ||
return mockstorage; | ||
}, | ||
// use null if your code does not use MESSAGING | ||
() => { | ||
return mockmessaging; | ||
} | ||
); | ||
// const mockauth = new firebasemock.MockAuthentication(); | ||
// const mockdatabase = new firebasemock.MockFirebase(); | ||
// const mockfirestore = new firebasemock.MockFirestore(); | ||
// const mockstorage = new firebasemock.MockStorage(); | ||
// const mockmessaging = new firebasemock.MockMessaging(); | ||
// const firebase = new firebasemock.MockFirebaseSdk( | ||
// // use null if your code does not use RTDB | ||
// (path: string) => { | ||
// return path ? mockdatabase.child(path) : mockdatabase; | ||
// }, | ||
// // use null if your code does not use AUTHENTICATION | ||
// () => { | ||
// return mockauth; | ||
// }, | ||
// // use null if your code does not use FIRESTORE | ||
// () => { | ||
// return mockfirestore; | ||
// }, | ||
// // use null if your code does not use STORAGE | ||
// () => { | ||
// return mockstorage; | ||
// }, | ||
// // use null if your code does not use MESSAGING | ||
// () => { | ||
// return mockmessaging; | ||
// } | ||
// ); | ||
|
||
export default firebase; | ||
// export default firebase; |
Oops, something went wrong.