Giter Site home page Giter Site logo

firepeer's Introduction

firepeer

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

Version Downloads/week License

Setup firebase

  1. Create a firebase project and setup the javascript client SDK.

  2. 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

  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.

    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

//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')
// 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 already connected!

Reference

https://natzcam.github.io/firepeer

Demo

https://firepeer-demo.firebaseapp.com

Source: https://github.com/natzcam/firepeer-demo

firepeer's People

Contributors

natzcam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.