replicant
Synchronize objects through a stream.
examples
join.js
Synchronize two objects with replicant.join()
var replicant = ;var a = ;var b = ;replicant;;;
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
join_stream.js
Synchronize two objects over a network stream
var replicant = ;var net = ;net;{var stream = net;}
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
methods
var replicant = require('replicant')
replicant.join(a, b)
Pipe replicant objects or streams to each other.
var update = replicant(obj)
Create a new replicant object wrapping an object obj
.
update(cb)
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
.
update.pipe(target)
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.
update.unpipe(target)
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.
attributes
update.object
Get the current state of the synchronized object. Only read this object and don't use its value to make updates.
events
update.on('update', function (obj) {})
Emitted when update
receives a patch. obj
is the new update.object
value.
install
With npm do:
npm install replicant
in the browser
This module works great in the browser with browserify.
tests
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.
license
MIT/X11