Synchronize objects through a stream.
Synchronize two objects with replicant.join()
var replicant = require('replicant');
var a = replicant({ a : 0 });
var b = replicant({ b : 100 });
replicant.join(a, b);
setInterval(function () {
a(function (obj) { obj.a ++ });
b(function (obj) { obj.b ++ });
}, 500);
setInterval(function () {
console.dir(a.object);
}, 1000);
ouput:
$ node example/join.js
{ a: 2, b: 102 }
{ a: 4, b: 104 }
{ a: 6, b: 106 }
{ a: 8, b: 108 }
{ a: 10, b: 110 }
^C
Synchronize two objects over a network stream
var replicant = require('replicant');
var net = require('net');
net.createServer(function (stream) {
var rep = replicant({ a : 0 });
replicant.join(rep, stream);
setInterval(function () {
rep(function (obj) { obj.a ++ });
}, 500);
setInterval(function () {
console.dir(rep.object);
}, 1000);
}).listen(5051, ready);
function ready () {
var stream = net.createConnection(5051, function () {
var rep = replicant({ b : 100 });
replicant.join(rep, stream);
setInterval(function () {
rep(function (obj) { obj.b ++ });
}, 300);
});
}
output:
$ node example/join_stream.js
{ a: 2, b: 103 }
{ a: 4, b: 106 }
{ a: 6, b: 110 }
{ a: 8, b: 113 }
{ a: 10, b: 116 }
^C
var replicant = require('replicant')
Pipe replicant objects or streams to each other.
Create a new replicant object wrapping an object obj
.
Update the synchronized object with the callback cb
.
If the arity of cb
is 1, cb(obj)
will be called. Just modify obj
in-place
in the same tick to have the modifications be broadcast to the other replicants.
If the arity of cb
is 2, cb(obj, emit)
will be called. Pass the emit
function the new version of the object obj
.
Pipe JSON data through the stream target
, which can be another replicant
object, a regular stream, or a replicant object's .pipe
function.
Using the object's .pipe
function here is handy for replicating over
dnode connections.
Don't pipe any more data to target
.
This is useful for when multiple clients are connected to the same replication server and the connection gets severed.
Get the current state of the synchronized object. Only read this object and don't use its value to make updates.
Emitted when update
receives a patch. obj
is the new update.object
value.
With npm do:
npm install replicant
This module works great in the browser with browserify.
To run the server-side tests in test/
, do:
npm test
To run the testling browser tests in testling/
, create
a testling account then from the project root directory run:
testling/test.sh
The shell script will prompt you to enter your testling account info.
MIT/X11