Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

PeerPouch

> 编程语言
Open source

PouchDB over WebRTC

195 stars0 likes2 views
WebsiteGitHub

About

PouchDB over WebRTC

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?

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言