downloads
Across all versions, last 30 days.
423 in 30 days
compatibility
Runtime metadata.
keywords
Tagged topics.
Across all versions, last 30 days.
423 in 30 days
Runtime metadata.
Tagged topics.
A two-phase-commit protocol for leveldb.
Provides strong-consistency for local-cluster replication.
Every node in your cluster can be writable and all reads from any node will be consistent.
Uses reconnect-core to support an injectable transport for e.g. browser compatibility.
The algorithm for how this works is here.
Estimated unpacked size across 43 packages.
6.9 MB
Includes dependencies.
ralphtheninja
ralphtheninja@riseup.net
var level = require('level');
var Replicator = require('level-2pc');
var net = require('net');
var db1 = level('./db', { valueEncoding: 'json' });
var opts = {
peers: [
{ host: 'localhost', port: 3001 },
{ host: 'localhost', port: 3002 }
]
};
var r = Replicator(db1, opts);
net.createServer(function(con) {
var server = r.createServer();
server.pipe(con).pipe(server);
}).listen(3000);var opts = {
peers: [
{ host: 'localhost', port: 3000 },
{ host: 'localhost', port: 3002 }
]
};
var r = Replicator(db2, opts);
net.createServer(function(con) {
var server = r.createServer();
server.pipe(con).pipe(server);
}).listen(3001);var opts = {
peers: [
{ host: 'localhost', port: 3000 },
{ host: 'localhost', port: 3001 }
]
};
var r = Replicator(db3, opts);
net.createServer(function(con) {
var server = r.createServer();
server.pipe(con).pipe(server);
}).listen(3002);Now go ahead and write some data to one of the servers and watch the data magically appear in the other servers!
setTimeout(function() {
db1.put('x', 100, function(err) {
console.log(err || 'ok');
});
setTimeout(function() {
db2.get('x', function() {
console.log(arguments);
db3.get('x', function() {
console.log(arguments);
});
});
}, 100);
}, 100);When the server wants to connect to the peers
that have been specified, it defaults to using
tcp from the net module. You can inject any
transportation layer you like by setting the
transport property in the options object:
var net = require('net');
var opts = {
transport: function() {
return net.connect.apply(null, arguments);
},
peers: [ /* .. */ ]
};
var r = Replicator(db, opts);Returns a Replicator object, which is an EventEmitter.
db leveldb database object
opts options object with the following properties:
host host that other peers should connect toport port that other peers should connect topeers an array of objects that specify the host and port of each peerminConsensus how many peers must connect initially or respond to quorumReturns a duplex rpc-stream that can be served over e.g. http or tcp or any other transport supporting node streams.
Closes connections to all peers.
Emitted when the replicator is ready to replicate with other peers. Happens when the replicator has enough connections for the quorum, i.e. when the number of peers is above minConsensus.
Emitted when the replicator is not ready to replicate with other peers. Happens when the replicator doesn't have enough connections for the quorum, i.e. when the number of peers goes below minConsensus.
Emitted when the replicator has connected to a peer.
host host of the connected peerport port of the connected peerEmitted when there was an error in the connection between the replicator and a peer.
err error objectEmitted when the replicator has disconnected from a peer.
host host of the disconnected peerport port of the disconnected peerEmitted when the replicator tries to reconnect to a peer.
host retrying connection to this hostport retrying connection to this portEmitted when the replicator has tried to reconnect but failed too many times. There might be a problem with the connection, or the peer is simply offline.
host host of the failing peerport port of the failing peer^1.3.0
10.0.0
~4.0.0
4.0.2
^2.0.1
2.0.4
^1.0.0
1.2.1
^3.1.0
3.1.1
^7.0.1
7.2.3
^2.1.2
2.1.2
^6.1.0
10.0.0
^1.0.1
1.3.0