Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/natzcam/firepeer

secure p2p signalling and authentication for simple-peer using firebase realtime database
https://github.com/natzcam/firepeer

browser firebase firebase-realtime-database node p2p peer signalling simplepeer webrtc

Last synced: about 1 month ago
JSON representation

secure p2p signalling and authentication for simple-peer using firebase realtime database

Awesome Lists containing this project

README

        

# firepeer

Secure p2p signalling and authentication for [simple-peer](https://github.com/feross/simple-peer) using [firebase realtime database](https://firebase.google.com/docs/database/).

[![Version](https://img.shields.io/npm/v/firepeer.svg)](https://npmjs.org/package/firepeer)
[![Downloads/week](https://img.shields.io/npm/dw/firepeer.svg)](https://npmjs.org/package/firepeer)
[![License](https://img.shields.io/npm/l/firepeer.svg)](https://github.com/natzcam/firepeer/blob/master/package.json)

## Setup firebase

1. [Create a firebase project and setup the javascript client SDK](https://firebase.google.com/docs/database/web/start).

2. Add these security rules in the firebase console to secure the signalling data.

```json
{
"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//database/rules`

3. Enable your prefered sign-in method in the firebase console. Firepeer requires authentication, so at the very least, you have to select [anonymous authentication](https://firebase.google.com/docs/auth/web/anonymous-auth).

> Shortcut: `https://console.firebase.google.com/project//authentication/providers`

### Install
```js
npm install --save firepeer
```
```html

```

### Usage

```javascript
//alice side

firebase.initializeApp({
//values from firebase console
});

const alice = new FirePeer(firebase);

console.log(alice.id) // peer id of alice

//authenticate with the sign-in method you enabled in the console
await firebase.auth().signInWith*()

console.log(alice.uid) // uid of alice

// connect
const connection = await alice.connect(BOB_UID, BOB_ID);

// send a mesage to bob
connection.send('hello')
```

```javascript
// bob side

firebase.initializeApp({
//values from firebase console
});

const bob = new FirePeer({
app: firebase.app()
});

console.log(bob.id) // id

//authenticate
await firebase.auth().signInWith*()

console.log(bob.uid) // peer id of bob

// wait for connection and receive message
bob.on('connection', (connection)=>{
connection.on('data', (data)=>{
console.log(data) //hello
})
})
```

Connections are just instances of [simple-peer](https://github.com/feross/simple-peer#api) already connected!

## Reference

[https://natzcam.github.io/firepeer](https://natzcam.github.io/firepeer)

## Demo

[https://firepeer-demo.firebaseapp.com](https://firepeer-demo.firebaseapp.com)

Source:
[https://github.com/natzcam/firepeer-demo](https://github.com/natzcam/firepeer-demo)