Skip to content

Latest commit

 

History

History
276 lines (188 loc) · 8.42 KB

README.md

File metadata and controls

276 lines (188 loc) · 8.42 KB

socket.io-pouch Build Status codecov

// This pouch is powered by web sockets!
var db = new PouchDB('mydb', {adapter: 'socket', url: 'ws://localhost:80'});

Adapter plugin that proxies all PouchDB API calls to another PouchDB running on the server in Node.js. The communication mechanism is Socket.io.

This means that instead of syncing over HTTP, socket.io-pouch syncs over WebSockets. Thanks to Engine.io, it falls back to XHR polling in browsers that don't support WebSockets.

The socket.io-pouch library has two parts:

  • A Node.js server, which can create local PouchDBs or proxy to a remote CouchDB.
  • A JavaScript client, which can run in Node.js or the browser.

This adapter passes the full PouchDB test suite. It requires PouchDB 5.0.0+.

Usage

$ npm install socket.io-pouch

Server

var socketPouchServer = require('socket.io-pouch/server');

socketPouchServer.listen(80);

Or you can attach to an already initialized socket.io socket server

var io = require('socket.io');
var socketPouchServer = require('socket.io-pouch/server');
var http = require('http');

var server = http.createServer();
var ioSock = io.listen(server);

socketPouchServer.listen(ioSock, {
    attach: true,
  });

Client

In the browser

When you npm install socket.io-pouch, the client JS file is available at node_modules/socket.io-pouch/dist/socket.io-pouch.client.js. Or you can just download it from Github above.

Then include it in your HTML, after PouchDB:

<script src="pouchdb.js"></script>
<script src="socket.io-pouch.client.js"></script>

Then you can create a socket-powered PouchDB using:

var db = new PouchDB('mydb', {
  adapter: 'socket',
  url: 'ws://localhost:80'
});
In Node.js/Browserify

The same rules apply, but you have to notify PouchDB of the new adapter:

var PouchDB = require('pouchdb');
PouchDB.adapter('socket', require('socket.io-pouch/client'));

API

Server

var socketPouchServer = require('socket.io-pouch/server');

socketPouchServer.listen(80, {}, function () {
  // server started
});

socketPouchServer.listen(port [, options] [, callback])

Arguments
  • port: the port to listen on. You should probably use 80 or 443 if you plan on running this in production; most browsers are finicky about other ports. 8080 may work in Chrome during debugging.
  • options: (optional) options object
    • attach: (optional) Boolean. Tells socket.io-pouch to use port variable as a socket server and attach to it.
    • remoteUrl: tells socket.io-pouch to act as a proxy for a remote CouchDB at the given URL (rather than creating local PouchDB databases)
    • pouchCreator: alternatively, you can supply a custom function that takes a string and returns any PouchDB object however you like. (See examples below.)
    • socketOptions: (optional) options passed verbatim to Engine.io. See their documentation for details.
  • callback: (optional) called when the server has started

Create a server which creates local PouchDBs, named by the user and placed in the current directory:

socketPouchServer.listen(80, {}, function () {
  console.log('server started!');
});

Create a server which acts as a proxy to a remote CouchDB (or CouchDB-compliant database):

socketPouchServer.listen(80, {
  remoteUrl: 'http://localhost:5984'
});

So e.g. when the user requests a database called 'foo', it will use a remote database at 'http://localhost:5984/foo'. Note that authentication is not handled, so you may want the pouchCreator option instead.

Create a MemDOWN-backed PouchDB server:

socketPouchServer.listen(80, {
  pouchCreator: function (dbName) {
    return new PouchDB(dbName, {
      db: require('memdown')
    });
  }
});

Note that this dbName is supplied by the client ver batim, meaning it could be dangerous. In the example above, everything is fine because MemDOWN databases can have any string as a name.

Alternatively, your pouchCreator can return a Promise if you want to do something asynchronously, such as authenticating the user. In that case you must wrap the object in {pouch: yourPouchDB}:

socketPouchServer.listen(80, {
  pouchCreator: function (dbName) {
    return doSomethingAsynchronously().then(function () {
      return {
        pouch: new PouchDB('dbname')
      };
    });
  }
});

Client

var db = new PouchDB({
  adapter: 'socket',
  name: 'mydb',
  url: 'ws://localhost:80',
  socketOptions: {}
});

The name and url are required and must point to a valid socketPouchServer. The socketOptions, if provided, are passed ver batim to Engine.io, so refer to their documentation for details.

Replication

The db object acts like a PouchDB that communicates remotely with the socketPouchServer In other words, it's analogous to a PouchDB created like new PouchDB('http://localhost:5984/mydb').

So you can replicate using the normal methods:

var localDB = new PouchDB('local');
var remoteDB = new PouchDB({adapter: 'socket', name: 'remote', url: 'ws://localhost:80'});

// replicate from local to remote
localDB.replicate.to(remoteDB);

// replicate from remote to local
localDB.replicate.from(remoteDB);

// replicate bidirectionally
localDB.sync(remoteDB);

For details, see the official replicate() or sync() docs.

Remote API

var remoteDB = new PouchDB({adapter: 'socket', name: 'remote', url: 'ws://localhost:80'});

You can also talk to this remoteDB as if it were a normal PouchDB. All the standard methods like info(), get(), put(), and putAttachment() will work. The Travis tests run the full PouchDB test suite.

Debugging

SocketPouch uses debug for logging. So in Node.js, you can enable debugging by setting a flag:

DEBUG=pouchdb:socket:*

In the browser, you can enable debugging by using PouchDB's logger:

PouchDB.debug.enable('pouchdb:socket:*');

Q & A

How does it communicate?

SocketPouch communicates using the normal Engine.io APIs like send() and on('message').

Normally it sends JSON text data, but in the case of attachments, binary data is sent. This means that SocketPouch is actually more efficient than regular PouchDB replication, which (as of this writing) uses base64-string encoding to send attachments between the client and server.

Does it work in a web worker or service worker?

Unfortuantely, not at the moment.

How is it implemented?

This is a custom PouchDB adapter. Other examples of PouchDB adapters include the built-in IndexedDB, WebSQL, LevelDB, and HTTP (Couch) adapters, as well as a partial adapter written for pouchdb-replication-stream and worker-pouch, which is a fork of this repo.

Changelog since we Forked

  • 1.0.0
    • Using Socket.io instead of pure websockets
    • Added attach to an already initialized socket.io server
    • Updated all vulnerable dependencies

Changelog of the Forked Repo

  • 2.0.0
    • Support for PouchDB 6.0.0, drop support for PouchDB <=5
  • 1.0.0
    • Initial release

Building

npm install
npm run build

Testing

In Node

This will run the tests in Node using LevelDB:

npm test

You can also check for 100% code coverage using:

npm run coverage

Run certain tests:

GREP=foo npm test

In the browser

Run npm run dev and then point your favorite browser to http://127.0.0.1:8000/test/index.html.

The query param ?grep=mysearch will search for tests matching mysearch.

Automated browser tests

You can run e.g.

CLIENT=selenium:firefox npm test
CLIENT=selenium:phantomjs npm test

This will run the tests automatically and the process will exit with a 0 or a 1 when it's done. Firefox uses IndexedDB, and PhantomJS uses WebSQL.