GithubHelp home page GithubHelp logo

peerpouch's Introduction

PeerPouch

A plugin for PouchDB which allows a remote PouchDB instance to be used locally. It transfers data over WebRTC, for simple lower-latency remote access and even particularly peer-to-peer replication!

API

To set up a peer connection, you actually need to start with a centralized database — this hub is first used for signalling connection parameters, and then the connection can be used to exchange messages.

PouchDB(anyDB, function (e, hub) { /* now you can use the methods below */ })

This isn't actually a PeerPouch call, but to get the "hub" below you simply create a PouchDB instance for a database that all peers will share. This would usually be a CouchDB server accessed via HTTPS, but PeerPouch itself doesn't really care. (For example, in testing I sometimes use IndexedDB as a hub between two tabs in the same browser.)

This hub is used for signalling, i.e. for exchanging information about shares and peers. Every share creates a corresponding document in the database. Additionally, when opening a share the peers exchange a number of messages via this hub, received via its changes feed.

hub.getSharedDatabases([opts, ]cb)

Returns an array of shares available (not including ones you have shared) to cb. You may also provide a callback via opts.onChange which will receive new/_deleted shares as they are updated.

Each of these share documents will be locally modified to include a dbname field. This can be used to intialize using the PeerPouch WebRTC adapter, e.g. PouchDB(share.dbname, function (e, remote) { /* now the remote share is locally accessible! */ }.

hub.shareDatabase(db[, opts], cb)

Allows you to share a local database to remote peers. Opts can include .name (intended to be a human-readble string), .info (any arbitrary JSON-stringifiable object).

You may also provide an event handler via opts.onRemote which gets info and can evt.preventDefault() to refuse connection. [This API is still in progress and may change. Also note that security is a bit weak as there's no way to actually verify the remote's identity without having trusted design document on a trusted hub.]

CAUTION: because PouchDB was designed for single-user application, any remote which connects to will have whatever privilege access you do to the share.

hub.unshareDatabase(db, cb)

Removes the share information for the given database from the hub.

Example

After including PouchDB and PeerPouch on your page, start by opening the hub:

PouchDB("http://peerpouch-test.ipcalf.com", function (e, hub) {
    // now you can serve a database using…
    hub.shareDatabase(/* someOtherLocalPouch */);
    
    // or connect to someone else's shared database like…
    hub.getSharedDatabases(function (e,shares) {
        PouchDB(shares[0].dbname, function (e, remote) {
            remote.allDocs(function (e,result) { console.log("Peer's documents are:", result.rows); });
            // or set up two-way replication to a local DB!
            PouchDB.replicate(remote, local);
            PouchDB.replicate(local, remote);
        });
    });
});

Demo

Check out sendfile?

peerpouch's People

Contributors

natevw avatar

Watchers

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