// 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 Engine.io, the famous core of Socket.io.
This means that instead of syncing over HTTP, socket-pouch syncs over WebSockets. Thanks to Engine.io, it falls back to XHR polling in browsers that don't support WebSockets.
The socket-pouch library has two parts:
This adapter passes the full PouchDB test suite. It requires PouchDB 5.0.0+.
$ npm install socket-pouch
var socketPouchServer = require('socket-pouch/server');
socketPouchServer.listen(80);
When you npm install socket-pouch
, the client JS file is available at node_modules/socket-pouch/dist/socket-pouch.client.js
. Or you can just download it from Github above.
Then include it in your HTML, after PouchDB:
<script src="https://github.com/pouchdb-community/socket-pouch/raw/master/pouchdb.js"></script>
<script src="https://github.com/pouchdb-community/socket-pouch/raw/master/socket-pouch.client.js"></script>
Then you can create a socket-powered PouchDB using:
var db = new PouchDB('mydb', {
adapter: 'socket',
url: 'ws://localhost:80'
});
The same rules apply, but you have to notify PouchDB of the new adapter:
var PouchDB = require('pouchdb');
PouchDB.adapter('socket', require('socket-pouch/client'));
var socketPouchServer = require('socket-pouch/server');
socketPouchServer.listen(80, {}, function () {
// server 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')
};
});
}
});
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.
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.
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.
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:*');
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.
Unfortuantely, not at the moment.
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.
npm install
npm run build
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
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
.
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.