level-replicate

scuttlebutt style replication for level-*

npm install level-replicate
20 downloads in the last week
23 downloads in the last month

level-replicate

master-master replication with levelup.

Implements scuttlebutt style handshake and then syncs data, then replicates real time changes.

Example

Replicate from a between two processes. One process starts a server, and another connects.

//master1.js
var level   = require('level')
var SubLevel  = require('level-sublevel')
var net       = require('net')
var Replicate = require('level-replicate')

//setup the database.
var db = SubLevel(level('/tmp/example-master'))

//install Master plugin!
var master = Replicate(db, 'master', "MASTER-1")

//create a server, and stream data to who ever connects.
net.createServer(function (stream) {
  stream.pipe(master.createStream({tail: true})).pipe(stream)
}).listen(9999, function () {
  console.log('master db listening on 9999')
})

Then, the code for the client!

//master2.js
var levelup   = require('level')
var SubLevel  = require('level-sublevel')
var net       = require('net')
var Replicate = require('level-replicate')

var db = SubLevel(level('/tmp/example-slave'))
var master = Replicate(db, 'master', "MASTER-2")

var stream = net.connect(9999)

stream.pipe(master.createStream({tail: true})).pipe(stream)

Wow, that was simple.

Binary data.

by default, level-replicate supports buffers by converting them to base64 via stream-serializer and json-buffer. If you desire more efficiency, use level-replicate/msgpack

var Replicate = require('level-replicate/msgpack')

var db = SubLevel(level('/tmp/example-slave'))
var master = Replicate(db, 'master', "MASTER-2")

License

MIT

npm loves you